简体   繁体   中英

How to append space at the end of Key and Values in the list of dictionary in python

I want to append space at the end of each key and value in the list of dictionary. I know this sounds odd but i have a requirement which need this kind of list of dictionaries. Any suggestions!

Example:

# 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}]

So here i need to append 10 space at the end of key and its value.

# 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            '}
       ]

also my list of dict always varies so my_list and position_list will change every time, so no way i can hard code the keys and the space specified.

You can simply do this:

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]

This outputs:

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

EDIT:

If you want your keys and your values to have the same spacing, simply use this ( positions[k] instead of postions[v] ).

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

I don't know why you need this. It violates PEP 0008

Anyway one way to do this is using using the str.format() method to format your key/value pairs. But first you will need to convert your "position_list" to a regular dict . Then iterate over the list of your dict s and format the items.

>>> 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              '}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM