简体   繁体   中英

How to update a dictionary with nested list using a dictionary?

I got the following dictionary, 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 = {
   "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 .

If CGR cinémas exist in network_values and theater_values , I will update the theater_values to 0 (integer or character possible).

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 .

Then, in the if statement you are currently comparing dictionaries with strings, so the expression is never true. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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