简体   繁体   English

如何快速从键列表和列表值构建python字典?

[英]How to build a python dictionary from a list of keys and a list values, quickly?

I have to lists 我必须列出

labels = ['normal.']
percentages = [0.9936]

I want to build a dictionary from these two lists 我想从这两个列表中建立字典

d = {}
for k, v in enumerate(lables, percentages):
    d[k] = v

But i'm getting the error: 但是我得到了错误:

TypeError: 'list' object cannot be interpreted as an integer

What could be wrong here ? 这有什么问题吗?

Edit 编辑

Then when i get the dict, i want to perform this operation 然后当我收到字典时,我想执行此操作

result = [str(k) + ": " + str(v) for k, v in previous_dict]

One way is to zip the two lists together, and convert the zipped object to a dictionary. 一种方法是压缩两个列表一起,并压缩的对象转换为一个字典。 After that you can iterate on dict.items() to create your list 之后,您可以在dict.items()上迭代以创建列表

In [158]: labels = ['normal.'] 
     ...: percentages = [0.9936]                                                                                                                                                    

In [159]: previous_dict = dict(zip(labels,percentages))  
In [159]: previous_dict                                                                                                                              
Out[159]: {'normal.': 0.9936}

In [24]: result = [str(k) + ": " + str(v) for k, v in previous_dict.items()]                                                                                                        

In [25]: result                                                                                                                                                                     
Out[25]: ['normal.: 0.9936']

Also enumerate gives you a list of tuples of type (index, element) , you cannot pass it two iterators like that but you can zip the two iterators again and make a dictionary, and the code will be as follows. 枚举为您提供类型(index, element)的元组列表,您不能像这样传递两个迭代器,但可以再次zip这两个迭代器并制作一个字典,代码如下。

For Python 3.6+ we can also use f-strings to format our string as well 对于Python 3.6+,我们也可以使用f字符串格式化字符串

In [167]: labels = ['normal.'] 
     ...: percentages = [0.9936]                                                                                                                                                    

In [169]: d = {} 
     ...: for k, v in zip(labels, percentages): 
     ...:     d[k] = v 

In [170]: d                                                                                                                                                                         
Out[170]: {'normal.': 0.9936}

In [30]: result = [f'{k}:{v}' for k, v in previous_dict.items()]                                                                                                                    

In [31]: result                                                                                                                                                                     
Out[31]: ['normal.:0.9936']

Here is an answer using list comprehension which you were trying to do. 这是您尝试使用列表理解的答案。 To get the second step working, you need to use .items() to access both, the key and the value 要使第二步正常工作,您需要使用.items()来访问键和值

labels = ['normal.']
percentages = [0.9936]

previous_dict = {k:v for k, v in zip(labels, percentages)}
# {'normal.': 0.9936}

result = [str(k) + ": " + str(v) for k, v in previous_dict.items()]
# ['normal.: 0.9936']

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

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