How to print list item + integer/string using logging in Python

前端 未结 2 492
攒了一身酷
攒了一身酷 2021-01-19 03:39

I\'d like to print list item with the index of item such as

0: [(\'idx\', 10), (\'degree\', 0)]
1: [(\'idx\', 20), (\'degree\', 0)]

Based o

相关标签:
2条回答
  • 2021-01-19 04:09

    I would do it like this.

    def funcA():
        a = []
        a.append(Node(10, 0))
        a.append(Node(20, 0))
    
        for i in range(0, len(a)):
            message = '%s:%s' % (i, a[i].items())
            logging.debug(message)
    

    Which produces this as output:

    DEBUG:root:0:[('idx', 10), ('degree', 0)]
    DEBUG:root:1:[('idx', 20), ('degree', 0)]
    

    You could also use join:

    message = ':'.join([str(i), str(a[i].items())])
    

    Or format:

    message = '{0}:{1}'.format(str(i), a[i].items())
    

    Whatever is the most clear to you.

    0 讨论(0)
  • 2021-01-19 04:11

    with Python > 3.6 you can use fstring

    logging.debug(f"{i}:{a[i].items()}")
    
    0 讨论(0)
提交回复
热议问题