繁体   English   中英

字符串列表列表到字典列表

[英]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.

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