假设我有一个Python字符串列表:
['Name: volume_test_add_volume_to_cg_2019_03_07-12_21_37', 'Index: 24',
'Name: volume_xx111', 'Index: 3', 'Name: volume_xx11541', 'Index: 4',
'Name: Volume_test_add_volume_mandatory_params_2019_03_06-16_50_10', 'Index: 6']
如何将它们转换为词典列表,以便最终结果如下:
[
{'Name': 'volume_test_add_volume_to_cg_2019_03_07-12_21_37', 'Index': '24'},
{'Name': 'volume_xx111', 'Index': '3'},
{'Name': 'volume_xx11541', 'Index': '4'},
{'Name': 'Volume_test_add_volume_mandatory_params_2019_03_06-16_50_10', 'Index': '6}
]
答案 0 :(得分:1)
您需要在代码中决定如何将字符串分组为字典。也许每个元素总是有2个元素,或者总是有一个Name
条目,或者您每次只需要看到一个键就只需要创建一个新字典。
如果每个词典中总是有 N 个元素,则iterate in chunks of that size:
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
results = [
dict(tuple(map(str.strip, entry.split(': '))) for entry in per_dict)
for per_dict in chunks(inputlist, 2)
]
演示:
>>> from pprint import pprint
>>> inputlist = ['Name: volume_test_add_volume_to_cg_2019_03_07-12_21_37', 'Index: 24', 'Name: volume_xx111', 'Index: 3', 'Name: volume_xx11541', 'Index: 4', 'Name: Volume_test_add_volume_mandatory_params_2019_03_06-16_50_10', 'Index: 6']
>>> def chunks(l, n):
... """Yield successive n-sized chunks from l."""
... for i in range(0, len(l), n):
... yield l[i:i + n]
...
>>> [
... dict(tuple(map(str.strip, entry.split(': '))) for entry in per_dict)
... for per_dict in chunks(inputlist, 2)
... ]
[{'Name': 'volume_test_add_volume_to_cg_2019_03_07-12_21_37', 'Index': '24'}, {'Name': 'volume_xx111', 'Index': '3'}, {'Name': 'volume_xx11541', 'Index': '4'}, {'Name': 'Volume_test_add_volume_mandatory_params_2019_03_06-16_50_10', 'Index': '6'}]
>>> pprint(_)
[{'Index': '24', 'Name': 'volume_test_add_volume_to_cg_2019_03_07-12_21_37'},
{'Index': '3', 'Name': 'volume_xx111'},
{'Index': '4', 'Name': 'volume_xx11541'},
{'Index': '6',
'Name': 'Volume_test_add_volume_mandatory_params_2019_03_06-16_50_10'}]
如果看到重复输入是更好的方法,则从包含空字典的列表result
开始;您可以将键值对添加到result[-1]
。然后一一处理您的字符串,在':'
字符上分割每个字符串以创建键和值对。如果在最近的词典中已经找到该密钥,请开始一个新的空密钥:
results = [{}]
for entry in inputlist:
key, value = map(str.strip, entry.split(':')) # removing surrounding whitespace
if key in results[-1]:
# start a new dictionary
results.append({})
results[-1][key] = value
通过检查是否存在key
,是否交换了Name
和Index
条目就不再重要了。
演示:
>>> results = [{}]
>>> for entry in inputlist:
... key, value = map(str.strip, entry.split(':')) # removing surrounding whitespace
... if key in results[-1]:
... # start a new dictionary
... results.append({})
... results[-1][key] = value
...
>>> pprint(results)
[{'Index': '24', 'Name': 'volume_test_add_volume_to_cg_2019_03_07-12_21_37'},
{'Index': '3', 'Name': 'volume_xx111'},
{'Index': '4', 'Name': 'volume_xx11541'},
{'Index': '6',
'Name': 'Volume_test_add_volume_mandatory_params_2019_03_06-16_50_10'}]
答案 1 :(得分:-1)
我的解决方法是:
lista = ['Name: volume_test_add_volume_to_cg_2019_03_07-12_21_37', 'Index: 24',
'Name: volume_xx111', 'Index: 3',
'Name: volume_xx11541', 'Index: 4',
'Name: Volume_test_add_volume_mandatory_params_2019_03_06-16_50_10', 'Index: 6']
result = []
for n, item in enumerate(lista):
if n % 2 == 0:
result.append({'Name': item[item.find(':') + 2:],
'Index': lista[n + 1][lista[n + 1].find(':') + 2:]})
print(result)
具有以下输出:
[{'Name': 'volume_test_add_volume_to_cg_2019_03_07-12_21_37', 'Index': '24'}, {'Name': 'volume_xx111', 'Index': '3'}, {'Name': 'volume_xx11541', 'Index': '4'}, {'Name': 'Volume_test_add_volume_mandatory_params_2019_03_06-16_50_10', 'Index': '6'}]