简体   繁体   English

如何将嵌套列表中的值更改为字典中指定的值?

[英]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'].而不是 [1,9,13] 我想要 ['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: 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:此外,这是 dict 存储的内容:

{'': 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. obs: header 是一个包含名称的列表。

Having trouble making out where you are starting from, but starting from your output of the list of integers and the dict at the end of your question, you could do this:无法确定您从哪里开始,但从整数列表的 output 和问题末尾的字典开始,您可以这样做:

inverted_dict = {restaurants_dict[k]:k for k in restaurants_dict}

rest_names = []
for i in range(len(list_of_restaurants_index)):
    row = []
    for j in range(len(list_of_restaurants_index[i])):
        row.append(inverted_dict[list_of_restaurants_index[i][j]])
    rest_names.append(row)

"rest_names" should be what you want, I think.我认为“rest_names”应该是你想要的。
This creates a new list, rather than editing the members of your original list.这将创建一个新列表,而不是编辑原始列表的成员。 Not sure if that matters to you.不确定这对你是否重要。

Not sure the purpose of the dict.不确定字典的目的。

empty_list.append(i[1]) seems to be appending a number empty_list.append(i[1])似乎在附加一个数字

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)

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

相关问题 如何将嵌套字典的值转换为 pandas dataframe? - How can I turn the values of a nested dict into a pandas dataframe? 如何按值和键的多个条件对嵌套字典进行排序? - How can I sort a nested dict by multiple criteria for values and keys? 我可以使用键列表访问嵌套的dict吗? - Can I access a nested dict with a list of keys? 如何从 dict 获取值列表? - How can I get list of values from dict? 如何将字典/嵌套字典的字典转换为列表字典 - How do I convert dict of dicts/nested dicts into dict of list 将列表中的值更改为字典中指定的值? - Change values in a list to those specified in a dictionary? 如何从列表中创建一个字典,其中值是列表的元素,键是python中这些元素的函数? - How to create a dict from list where values are elements of the list and keys are function of those elements in python? Python - 如何将嵌套字典的所有值作为单个 List[dict] 获取? - Python - how to get all the values of nested dictionaries as a single List[dict]? 如何只按字母顺序打印嵌套在列表中,字典中的内部词典的键? - How can I print only the keys of inner dictionaries that are nested within a list, within a dict, in alphabetical order? 如何将列表的值插入嵌套列表? - How can I insert values of a list into a nested list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM