[英]List of lists of strings to a list of dictionaries
我有一長串格式如下的列表:
[
[
'iyr:1928',
'cid:150',
'pid:476113241',
'eyr:2039',
'hcl:a5ac0f',
'ecl:#25f8d2',
'byr:2027',
'hgt:190'
],
[
'hgt:168cm',
'eyr:2026',
'ecl:hzl',
'hcl:#fffffd',
'cid:169',
'pid:920076943',
'byr:1929',
'iyr:2013'
],
...
]
我想把它轉換成這樣的字典列表:
[
{
"iyr": "1928",
"cid": "150",
"pid": "476113241",
"eyr": "2039",
"hcl": "a5ac0f",
"ecl": "#25f8d2",
"byr": "2027",
"hgt": "190"
},
{
"hgt": "168cm",
"eyr": "2026",
"ecl": "hzl",
"hcl": "#fffffd",
"cid": "169",
"pid": "920076943",
"byr": "1929",
"iyr": "2013"
},
...
]
我知道我必須split(':')
每個列表中的每個條目,但我不知道如何將該塊轉換為字典。
[{entry.split(':')[0] : entry.split(':')[1] for entry in entries} for entries in myarray]
您可以 go 覆蓋列表中的每個項目,然后對於每個項目(這是一個列表), go 覆蓋其項目,將它們拆分為:
如您所建議的那樣。)並使用第一個項目作為鍵,第二個作為值.
使用 list 和 dict 理解,這甚至可以是一個單行:
result = [{s.split(':')[0]:s.split(':')[1] for s in l} for l in lst]
我喜歡簡短但簡單易讀的版本,所以我可能會這樣做:
make_dict = lambda list_item : {x.split(':')[0]:x.split(':')[1] for x in list_item}
l1 = [make_dict(item) for item in org_list]
連同其他更簡潔的答案,這里有一個更詳細的版本,它做同樣的事情
def list_to_dict(elements):
elem_dict = {}
for elem in elements:
k, v = elem.split(":")
elem_dict[k] = v
return elem_dict
list_of_lists = [
[
'iyr:1928',
'cid:150',
'pid:476113241',
'eyr:2039',
'hcl:a5ac0f',
'ecl:#25f8d2',
'byr:2027',
'hgt:190'
],
[
'hgt:168cm',
'eyr:2026',
'ecl:hzl',
'hcl:#fffffd',
'cid:169',
'pid:920076943',
'byr:1929',
'iyr:2013'
],
]
list_of_dicts = map(list_to_dict, list_of_lists)
print(list_of_lists)
print(list_of_dicts)
這個答案也考慮了多個:
在您的字符串中,並且不依賴於索引列表:
lol = [
[
'iyr:1928',
'cid:150',
'pid:476113241',
'eyr:2039',
'hcl:a5ac0f',
'ecl:#25f8d2',
'byr:2027',
'hgt:190'
],
[
'hgt:168cm',
'eyr:2026',
'ecl:hzl',
'hcl:#fffffd',
'cid:169',
'pid:920076943',
'byr:1929',
'iyr:2013'
]
]
res = [dict([tuple(i.split(':', maxsplit=1)) for i in l]) for l in lol]
print(res)
'''
[
{
"iyr":"1928",
"cid":"150",
"pid":"476113241",
"eyr":"2039",
"hcl":"a5ac0f",
"ecl":"#25f8d2",
"byr":"2027",
"hgt":"190"
},
{
"hgt":"168cm",
"eyr":"2026",
"ecl":"hzl",
"hcl":"#fffffd",
"cid":"169",
"pid":"920076943",
"byr":"1929",
"iyr":"2013"
}
]
'''
cProfiling 產量:
cProfile.run('res = [dict([tuple(i.split(":", maxsplit=1)) for i in l]) for l in lol]')
20 function calls in 0.000 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 <string>:1(<listcomp>)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
16 0.000 0.000 0.000 0.000 {method 'split' of 'str' objects}
與產生的公認解決方案相比:
cProfile.run('res = [{entry.split(":")[0] : entry.split(":")[1] for entry in entries} for entries in myarray]')
38 function calls in 0.000 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
2 0.000 0.000 0.000 0.000 <string>:1(<dictcomp>)
1 0.000 0.000 0.000 0.000 <string>:1(<listcomp>)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
32 0.000 0.000 0.000 0.000 {method 'split' of 'str' objects}
因此,我提出的解決方案確實可以更好地擴展 OP 提到的大型列表列表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.