python - 如何使用 Python 中的日志打印列表项 + 整数/字符串

标签 python python-2.7

我想打印带有项目索引的列表项目,例如

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

根据下面的代码,如何将“0:”附加为整数 + 字符串 + 列表项?

import logging

class Node(object):
    __slots__= "idx", "degree"

    def __init__(self, idx, degree):
        self.idx = idx
        self.degree = 0


    def items(self):
        "dict style items"
        return [
            (field_name, getattr(self, field_name))
            for field_name in self.__slots__]

def funcA():

    a = []
    a.append(Node(10, 0))
    a.append(Node(20, 0))

    for i in range(0, len(a)):
        logging.debug(a[i].items())

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)  
    funcA()

目前,结果是

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

期待

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

最佳答案

使用 Python > 3.6 你可以使用 fstring

logging.debug(f"{i}:{a[i].items()}")

关于python - 如何使用 Python 中的日志打印列表项 + 整数/字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17690057/

相关文章:

python - hex() 参数无法转换为十六进制

python - 谷歌应用引擎开发服务器(python)上的自动任务执行

python - python中不加控制符打印的方法

Python:比较列表中的元素并打印具有最大匹配计数的元素

python - 对矩阵中的多个数组连续执行高斯拟合并保存结果

javascript - 通过ajax将上传的excel文件数据传递给python

python - GAE 中的 fetch() 获取什么?

python 2.7 : Simple 'for' loop implementation

python - Theano:在哪里放置 Anaconda 安装的 .theanorc 文件? ( Windows )

python - 我正在尝试使用 beautifulsoup 从 craigslist 中提取一些链接,但它拉动链接 100 次而不是一次