简体   繁体   English

如何使用字典更新带有嵌套列表的字典?

[英]How to update a dictionary with nested list using a dictionary?

I got the following dictionary, network_values我得到了以下字典network_values

network_values = {
    0: "CGR cinémas",
    1: "Ville de Choisy-le-Roi",
    2: "Pathé Gaumont",
    3: "UGC",
    4: "Cinéode",
    5: "Magestic",
    6: "Cinéville",
    7: "cinebus",
    8: "CGR",
    9: "Utopia",
    10: "Écran mobile 74"
}

I've another dictionary, theater_values , with nested lists我有另一个字典, theater_values ,嵌套列表

theater_values = {
   "0":[
      "node/4522090127",
      1.0,
      300.0,
      "Saint-Jean-Pied-de-Port",
      "nan"
   ],
   "1":[
      "node/563839797",
      2.0,
      239.0,
      "Barbezieux-Saint-Hilaire",
      "nan"
   ],
   "2":[
      "node/5472582580",
      "nan",
      "nan",
      "Lezay",
      "nan"
   ],
   "3":[
      "node/7623662713",
      2.0,
      280.0,
      "Montaigu-Vendée",
      "nan"
   ],
   "4":[
      "node/4115072853",
      1.0,
      274.0,
      "Chalonnes-sur-Loire",
      "nan"
   ],
   "5":[
      "node/5250851417",
      3.0,
      341.0,
      "Thiers",
      "nan"
   ],
   "6":[
      "way/83676895",
      3.0,
      534.0,
      "Argentan",
      "nan"
   ],
   "7":[
      "node/502262289",
      8.0,
      1898.0,
      "Tours",
      "CGR cinémas"
   ],
   "8":[
      "node/3475038904",
      "nan",
      "nan",
      "Saint-Étienne",
      "nan"
   ],
   "9":[
      "way/146090090",
      "nan",
      "nan",
      "Marcoussis",
      "nan"
   ],
   "10":[
      "way/66143799",
      "nan",
      "nan",
      "Choisy-le-Roi",
      "Ville de Choisy-le-Roi"
   ],
   "11":[
      "node/7768900435",
      12.0,
      2139.0,
      "Paris",
      "Pathé Gaumont"
   ]
}

The aim is when the value like CGR cinémas exist in the nested list of theater_values and also network_values , I update theater_values with the keys of network_values .目的是当像CGR cinémas cinémas 这样的值存在于theater_valuesnetwork_values的嵌套列表中时,我使用network_values的键更新theater_values

If CGR cinémas exist in network_values and theater_values , I will update the theater_values to 0 (integer or character possible).如果网络值和theater_valuesnetwork_values CGR cinémas ,我会将theater_values值更新为0 (可能是整数或字符)。

This is what I've done but it is not doing what it should这就是我所做的,但它没有做它应该做的事情

def proto_csv(theater_values):
    network_values = {0: 'CGR cinémas', 1: 'Ville de Choisy-le-Roi', 2: 'Pathé Gaumont', 3: 'UGC', 4: 'Cinéode', 5: 'Magestic', 6: 'Cinéville', 7: 'cinebus', 8: 'CGR', 9: 'Utopia', 10: 'Écran mobile 74}
    for keys_theaters,vals_theaters in theater_values.items():
        for keys_networks,vals_networks in network_values.items():
            if vals_theaters == vals_networks:
                theater_values[keys_theaters] = str(keys_networks)
    return theater_values

I'm a bit confused on how to do it as my code is not giving me the desired results or any results actually...我对如何做到这一点有点困惑,因为我的代码实际上并没有给我想要的结果或任何结果......

The updated result would be like that更新的结果是这样的

theater_values = {
       "0":[
          "node/4522090127",
          1.0,
          300.0,
          "Saint-Jean-Pied-de-Port",
          "nan"
       ],
       "1":[
          "node/563839797",
          2.0,
          239.0,
          "Barbezieux-Saint-Hilaire",
          "nan"
       ],
       "2":[
          "node/5472582580",
          "nan",
          "nan",
          "Lezay",
          "nan"
       ],
       "3":[
          "node/7623662713",
          2.0,
          280.0,
          "Montaigu-Vendée",
          "nan"
       ],
       "4":[
          "node/4115072853",
          1.0,
          274.0,
          "Chalonnes-sur-Loire",
          "nan"
       ],
       "5":[
          "node/5250851417",
          3.0,
          341.0,
          "Thiers",
          "nan"
       ],
       "6":[
          "way/83676895",
          3.0,
          534.0,
          "Argentan",
          "nan"
       ],
       "7":[
          "node/502262289",
          8.0,
          1898.0,
          "Tours",
          "O" #here
       ],
       "8":[
          "node/3475038904",
          "nan",
          "nan",
          "Saint-Étienne",
          "nan"
       ],
       "9":[
          "way/146090090",
          "nan",
          "nan",
          "Marcoussis",
          "nan"
       ],
       "10":[
          "way/66143799",
          "nan",
          "nan",
          "Choisy-le-Roi",
          "Ville de Choisy-le-Roi"
       ],
       "11":[
          "node/7768900435",
          12.0,
          2139.0,
          "Paris",
          "1" #here
       ]
    }

First thing is to close the quotation marks for 'Écran mobile 74 in your dictionary as the code you pasted returns SyntaxError .首先是关闭字典中'Écran mobile 74的引号,因为您粘贴的代码返回SyntaxError

Then, in the if statement you are currently comparing dictionaries with strings, so the expression is never true.然后,在if语句中,您当前正在将字典与字符串进行比较,因此表达式永远不会为真。 You want to see if the string is in the dictionary values and update that element of the dictionary if so:您想查看字符串是否在字典值中,如果是,则更新字典的该元素:

if vals_networks in vals_theaters:
    theater_values[keys_theaters][vals_theaters.index(vals_networks)] = str(keys_networks)

This gives the output:这给出了 output:

{
    '0': ['node/4522090127', 1.0, 300.0, 'Saint-Jean-Pied-de-Port', 'nan'],
    '1': ['node/563839797', 2.0, 239.0, 'Barbezieux-Saint-Hilaire', 'nan'],
    '2': ['node/5472582580', 'nan', 'nan', 'Lezay', 'nan'],
    '3': ['node/7623662713', 2.0, 280.0, 'Montaigu-Vendée', 'nan'],
    '4': ['node/4115072853', 1.0, 274.0, 'Chalonnes-sur-Loire', 'nan'],
    '5': ['node/5250851417', 3.0, 341.0, 'Thiers', 'nan'],
    '6': ['way/83676895', 3.0, 534.0, 'Argentan', 'nan'],
    '7': ['node/502262289', 8.0, 1898.0, 'Tours', '0'],
    '8': ['node/3475038904', 'nan', 'nan', 'Saint-Étienne', 'nan'],
    '9': ['way/146090090', 'nan', 'nan', 'Marcoussis', 'nan'],
    '10': ['way/66143799', 'nan', 'nan', 'Choisy-le-Roi', '1'],
    '11': ['node/7768900435', 12.0, 2139.0, 'Paris', '2']
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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