Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i changes the values in a nested list to those specified in a dict?

I have this code in which I have a list of lists and inside the sub-lists we got numbers. However, I also have a dict storing key-value pairs of name-number, in which the number is the index of the name stored in another list, but I want to replace all the numbers in the nested list with their respective names. Instead of having [1,9,13] I want to have [‘The Beach Chimney’, ‘Parlay’, ‘The Private Exhibit’].

out = [(ind,ind2) for ind,i in enumerate(rows) 
                  for ind2,y in enumerate(i) if y == '1']

list_of_restaurants_index = []
for empty_list in range(100):
  empty_list = []
  list_of_restaurants_index.append(empty_list)

for i in out:
  for empty_list in list_of_restaurants_index:
    if i[0] == list_of_restaurants_index.index(empty_list):
      empty_list.append(i[1])
print(list_of_restaurants_index)

restaurants_dict = {}
for i in header:
  restaurants_dict[i] = header.index(i)

Output:

[[1, 9, 13], [8, 14, 17], [6, 14], [4, 8, 9, 12], [1, 2, 5, 6, 12, 13, 18], [1, 4, 5, 8, 10, 11, 12], [1, 12, 14, 16], [3, 4, 5, 13, 14, 17, 18], [4, 9, 10, 12, 13, 14, 15, 18], [3, 16], [1, 3, 5, 10, 13], [1, 5, 10, 15, 17], [2, 4, 6, 7, 10, 13], [2, 4, 9, 12], [9, 11, 12, 13], [1, 3, 4, 7, 9, 17], [6, 8, 9, 10, 11, 13, 14, 15, 18], [2, 4, 6, 7, 8, 13], [4, 7, 10, 11, 13, 14, 16, 18], [7, 12, 16, 17, 18], [3, 7, 10, 13, 14], [1, 2, 3, 5, 8, 12, 14, 17, 18], [8, 10, 12, 13, 16, 17], [4, 5, 6, 7, 8, 11, 17], [1, 4, 7, 9, 13, 16], [4, 10, 12, 14, 15], [2, 3, 4, 11, 13, 14, 15, 17], [1, 2, 7, 8, 9, 16], [5, 6, 7], [4, 5, 6], [10, 16, 18], [6, 13, 14, 17], [1, 6, 16, 17], [1, 2, 4, 5, 10, 12, 15, 18], [3, 4, 10, 12, 14, 16, 18], [1, 2, 6, 8, 9, 11, 13, 14, 16, 17], [5, 6, 7], [1, 2, 5, 10, 13, 18], [4, 5, 13, 18], [5, 7, 9, 10, 11, 12, 15, 17], [2, 14], [4, 5, 9, 11, 12, 14], [1, 3, 5, 10, 11, 15], [3, 12, 13, 16], [1, 4, 5, 6, 7, 9, 11, 13, 14, 15], [4, 6, 9, 13, 15, 16, 18], [2, 3, 4, 6, 9, 12, 14, 15], [1, 7, 12, 14, 17], [5, 6, 8, 10, 11, 12, 15, 18], [2], [2], [4, 5, 7, 11, 12, 14], [1, 4, 9, 14, 18], [3, 6, 8, 15, 18], [1, 4, 7, 8, 14], [3, 4, 7, 11, 15], [1, 4, 5, 6, 10, 13, 14, 15, 16, 18], [2], [2], [5, 6, 8, 14, 15, 16, 18], [6], [8, 10, 12, 16], [1, 4, 5, 10, 14, 17, 18], [6], [5, 6, 9, 10, 13, 14], [1, 11, 12, 17], [1, 5, 10, 14, 15], [3, 4, 6, 9, 10, 15, 18], [4, 8, 10, 16, 17, 18], [4, 7, 14, 17], [1, 2, 3, 9, 16], [10, 12, 14, 16], [1, 2, 8, 10, 15], [1, 2, 4, 10, 13, 17], [3, 7, 17], [4, 5, 6], [4, 6, 11, 12, 17], [1, 2, 7, 8, 13, 16], [1, 2, 4, 8, 13], [8, 11], [1, 4, 5, 6, 7, 18], [1, 4, 5, 11, 12, 14, 17], [5, 8, 9, 14], [2], [4, 5, 7, 10, 14, 16], [6], [1, 2, 3, 4, 7, 11, 13, 14], [7, 12, 14], [3, 4, 7, 11, 13], [1, 2, 3, 4, 6, 7, 8, 9, 14, 15], [2], [6], [5, 12, 15, 16, 17, 18], [3, 4, 5, 11, 12, 13], [3, 5, 6, 14, 17, 18], [9, 12, 13, 15], [4, 14, 15, 16, 18], [4, 8], [3], [3]]

Also, here’s what the dict is storing:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{'': 0, 'The Beach Chimney': 1, "The Pirate's Harvest": 2, 'The Square Dragon': 3, 'The Spicy Trumpet': 4, 'Vertigo': 5, 'Drifters': 6, 'The Tulip': 7, 'Seawise': 8, 'Parlay': 9, 'The Modern Salmon': 10, 'The Bitter Windmill': 11, 'The Minty Window': 12, 'The Private Exhibit': 13, 'Enigma': 14, 'The Lighthouse': 15, 'Harlequin': 16, 'Midnight': 17, 'Gastrognome': 18}

obs: header is a list containing the names.

>Solution :

Not sure the purpose of the dict.

empty_list.append(i[1]) seems to be appending a number

That number happens to align with the indicies of the strings you want.

Therefore, you could instead use

name = header[i[1]]
empty_list.append(name)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading