Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中将字符串列表转换为字典列表_Python_List Comprehension_Dictionary Comprehension - Fatal编程技术网

如何在python中将字符串列表转换为字典列表

如何在python中将字符串列表转换为字典列表,python,list-comprehension,dictionary-comprehension,Python,List Comprehension,Dictionary Comprehension,假设我有一个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'] 如何将

假设我有一个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}
]

您需要在代码中决定如何将字符串分组到字典中。可能每个元素总是有2个元素,或者总是有一个名称条目,或者每次看到一个键之前,您只需要创建一个新的字典

如果每个字典始终有N个元素,则:

演示:

如果看到键重复是更好的方法,那么从包含空字典的列表结果开始;将键值对添加到结果[-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
通过检查键是否存在,名称和索引项是否被交换就不再重要了

演示:


我的解决方案如下:

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

解决这个问题的方法太多了。答案将变成一个人们喜欢的民意测验。最好的办法是自己做一些关于这个话题的研究,找到两三个,分析它们,确定它们是否对你有用,并尝试一下。当你有关于你试图做的事情的具体问题时,来找我们。
>>> 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'}]
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'}]