python - 字符串列表列表到字典列表

标签 python list dictionary

我有一长串格式如下的列表:

[
  [
    '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(':') 每个列表中的每个条目,但我不知道如何将该 block 转换为字典。

最佳答案

  [{entry.split(':')[0] : entry.split(':')[1] for entry in entries} for entries in myarray]

关于python - 字符串列表列表到字典列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65155606/

相关文章:

python - 将文件复制到目录时出现 FileNotFoundError

python 连接、映射和 lambda 方法

python - 通过 Python 发送 Outlook 电子邮件?

java - 访问 Java 中的字典元素,其中值是列表

python - 如何对字符串和 int 值数组、按字母顺序和 "reversed"字母顺序的多个属性进行排序

python - 有没有办法在 python 中使用 opencv 获得更清晰和连续的边缘?

html - css direct-child 选择器选择所有级别的所有 child

c++ - 无法从 map 获取字符分类信息

vba - 在 Visual Basic for Applications (VBA) 中设置全局 Scripting.Dictionary

python - 从 Python 字典中访问多个数据