如何在Python字典列表中的键和值末尾添加空格

2024-04-25 21:06:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在字典列表中每个键和值的末尾加上空格。我知道这听起来很奇怪,但我有一个要求,那就是需要这种词典列表。有什么建议吗!你知道吗

示例:

# 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列表每次都会改变,所以我无法硬编码指定的键和空间。你知道吗


Tags: ofthe列表dictionarytablecolumnlistint
2条回答

您可以简单地执行以下操作:

from collections import defaultdict
my_list = [{'Table': 'A', 'Column': 'C1', 'DataType': 'int'},
           {'Table': 'A', 'Column': 'C2', 'DataType': 'varchar'},
           {'Table': 'B', 'Column': 'C3', 'DataType': 'int'}]

position_list = [{'Table': 10}, {'Column': 15}, {'DataType': 10}]
positions = defaultdict(lambda : 10)
for d in position_list:
    positions.update(d)

print [{k + ' ' * positions[k] : v + ' ' * positions[v] for k,v in d.items()} for d in my_list]

这将输出:

[{'DataType          ': 'int          ', 'Table          ': 'A          ', 'Column               ': 'C1          '}, {'DataType          ': 'varchar          ', 'Table          ': 'A          ', 'Column               ': 'C2          '}, {'DataType          ': 'int          ', 'Table          ': 'B          ', 'Column               ': 'C3          '}]

编辑:

如果希望键和值具有相同的间距,只需使用(positions[k]而不是postions[v])。你知道吗

print [{k + ' ' * positions[k] : v + ' ' * positions[k] for k,v in d.items()} for d in my_list]

我不知道你为什么需要这个。它违反了PEP 0008

无论如何,一种方法是使用^{}方法格式化键/值对。但首先,您需要将“位置列表”转换为常规的dict。然后遍历dict的列表并格式化这些项。你知道吗

>>> my_list = [{'Column': 'C1', 'DataType': 'int', 'Table': 'A'},
...  {'Column': 'C2', 'DataType': 'varchar', 'Table': 'A'},
...  {'Column': 'C3', 'DataType': 'int', 'Table': 'B'}]
>>> position_list = [{'Table': 10}, {'Column': 15}, {'DataType': 10}]
>>> d = {k:v for item in position_list for k, v in item.items()}
>>> res = []
>>> for element in my_list:
...     item = {}
...     for key, value in element.items():
...         width = len(key) + d[key]
...         item["{:<{width}}".format(key, width=width)] = "{:<{width}}".format(value, width=width)
...     res.append(item)
... 
>>> import pprint # Just to pretty print res nothing special.
>>> pprint.pprint(res)
[{'Column               ': 'C1                   ',
  'DataType          ': 'int               ',
  'Table          ': 'A              '},
 {'Column               ': 'C2                   ',
  'DataType          ': 'varchar           ',
  'Table          ': 'A              '},
 {'Column               ': 'C3                   ',
  'DataType          ': 'int               ',
  'Table          ': 'B              '}]

相关问题 更多 >