我想在字典列表中每个键和值的末尾加上空格。我知道这听起来很奇怪,但我有一个要求,那就是需要这种词典列表。有什么建议吗!你知道吗
示例:
# Original List of dictionary
my_list = [{'Table': 'A', 'Column': 'C1', 'DataType': 'int'},
{'Table': 'A', 'Column': 'C2', 'DataType': 'varchar'},
{'Table': 'B', 'Column': 'C3', 'DataType': 'int'}
]
# Have another list of dictionary which specifies the space needs to add at the end of each key and value pair.
position_list = [{'Table': 10}, {'Column': 15}, {'DataType': 10}]
所以这里我需要在键的末尾加上10个空格和它的值。你知道吗
# Expected out put (Spaces may not be exact as shown below but expecting something like.)
list = [{'Table ': 'A ', 'Column ': 'C1 ', 'DataType ': 'int '},
{'Table ': 'A ', 'Column ': 'C2 ', 'DataType ': 'varchar '},
{'Table ': 'B ', 'Column ': 'C3 ', 'DataType ': 'int '}
]
而且我的dict列表总是不同的,所以我的\u列表和位置\u列表每次都会改变,所以我无法硬编码指定的键和空间。你知道吗
您可以简单地执行以下操作:
这将输出:
编辑:
如果希望键和值具有相同的间距,只需使用(
positions[k]
而不是postions[v]
)。你知道吗我不知道你为什么需要这个。它违反了PEP 0008
无论如何,一种方法是使用^{} 方法格式化键/值对。但首先,您需要将“位置列表”转换为常规的
dict
。然后遍历dict
的列表并格式化这些项。你知道吗相关问题 更多 >
编程相关推荐