简体   繁体   English

如何将值列表添加到字典中,其中列表中的第一个元素属于第一个键等等......?

[英]How can I add a list of values to a dictionary where the first element in the list belongs to the first key and so on...?

I have a dictionary which has predefined keys:我有一本具有预定义键的字典:

{1: None, 2: None, 3: None, 4: None, 5: None, 6: None, 7: None, 8: None}

and a list with numeric values:和一个带有数值的列表:

[0, 10, 20, 30, 40, 50, 60, 100] 

I want to add these values from the list to the dictionary so that it will look like this:我想将列表中的这些值添加到字典中,以便它看起来像这样:

{1: 0, 2: 10, 3: 20, 4: 30, 5: 40, 6: 50, 7: 60, 8: 100}

How can I add every item from the list to the next key of my dictionary?如何将列表中的每个项目添加到字典的下一个键?

You directly use zip on the dict and the list of values, tha make pairs, then ues dict to make mappings from pairs您直接在 dict 和值列表上使用zip进行配对,然后使用dict从对进行映射

d = {1: None, 2: None, 3: None, 4: None, 5: None, 6: None, 7: None, 8: None}
v = [0, 10, 20, 30, 40, 50, 60, 100]

res = dict(zip(d, v))

print(res) # {1: 0, 2: 10, 3: 20, 4: 30, 5: 40, 6: 50, 7: 60, 8: 100}

You could find strange to passe the whole dict d to zip but iterating over th dict is same as iterating over its key in fact, the following can have more sense but it does the same您可能会发现将整个字典d传递给zip很奇怪,但迭代字典实际上与迭代其键相同,以下内容可能更有意义,但它的作用相同

dict(zip(d.keys(), v))

Below is more dynamic solution where you dont have prepare half populated dict下面是更动态的解决方案,您没有准备半人口的字典

a_list = [0, 10, 20, 30, 40, 50, 60, 100] 
a_dict = {idx+1:val for idx,val in enumerate(a_list)}
print(a_dict)

output output

{1: 0, 2: 10, 3: 20, 4: 30, 5: 40, 6: 50, 7: 60, 8: 100}

I have a dictionary which has predefined keys:我有一个具有预定义键的字典:

{1: None, 2: None, 3: None, 4: None, 5: None, 6: None, 7: None, 8: None}

and a list with numeric values:和一个带有数值的列表:

[0, 10, 20, 30, 40, 50, 60, 100] 

I want to add these values from the list to the dictionary so that it will look like this:我想将列表中的这些值添加到字典中,使其看起来像这样:

{1: 0, 2: 10, 3: 20, 4: 30, 5: 40, 6: 50, 7: 60, 8: 100}

How can I add every item from the list to the next key of my dictionary?如何将列表中的每个项目添加到字典的下一个键?

暂无
暂无

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

相关问题 如何根据条件将第二个字典列表的键及其值添加到字典的第一个列表中 - How shall I add one of the key and its values of second list of dictionary to the first list of dictionary based on the condition 如何创建具有 2 个键的字典,其中第一个键是索引,第二个键来自列表,值来自 df 的列? - How can I create a dictionary with 2 keys where the the first key is the index, the second key is from a list and the values from columns of a df? 如何将两个列表的值存储在单个字典中,其中第一个列表的值是键,第二个列表的值是属性? - How to store the values of two lists in a single dictionary where values of first list is a key and the values of second list are attributes? 如何将元组列表写入到csv文件中,其中元组的第一个元素是字典? - How can I write a list of tuples to a csv file where the first element of the tuple is a dictionary? 如何将列表列表合并到字典中,其中嵌套列表的第一个元素是键 - How to combine list of lists into a dictionary where the first element of a nested list is the key 如何更改属于字典的列表中元素的值? - How can I change the value of an element in a list which belongs to a dictionary? 如何使用嵌套列表的第一个元素打印字典键 - how to print dictionary key with first element of nested list 如何检索列表中第一个元素的唯一值? - How can I retrieve the unique values of the first element of a list? 如何检查列表中每个元组的第一个元素是否是字典的键? - How to check if the first element of each tuple inside a list is a key of a dictionary? 如何将列表转换为字典,其中第一个列表是键,第二个列表是字典中的值? - how to convert the list to dictionary where first list is key and second list is value in dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM