列表中的字典 [英] a dictionary from a list
问题描述
我知道必须有一个更好的方式来表达这一点,所以google理解,但
我不知道怎么样......所以我会问别人。
假设我有一个名为''alist''的列表对象。
有一种简单的方法来创建一个包含
$成员的字典对象b $ b''alist''是字典中的键,键的值设置为
null?
-
David Bear
- 让我购买你的知识产权,我想拥有你的想法 -
I know there must be a better way to phrase this so google understands, but
I don''t know how.. So I''ll ask people.
Assume I have a list object called ''alist''.
Is there an easy way to create a dictionary object with the members of
''alist'' being the keys in the dictionary, and the value of the keys set to
null?
--
David Bear
-- let me buy your intellectual property, I want to own your thoughts --
推荐答案
dict((x,None)for al in alist)
dict((x, None) for x in alist)
David Bear写道:
David Bear wrote:
假设我有一个列表对象称为''alist''。
是否有一种简单的方法来创建一个字典对象,其中
''alist''的成员是字典中的键,值和值的键设置为
null?
Assume I have a list object called ''alist''.
Is there an easy way to create a dictionary object with the members of
''alist'' being the keys in the dictionary, and the value of the keys set to
null?
你的意思是没有,对吧? :)
You mean None, right? :)
a_list = [1,2,3,''a'',''b'' ,''c'']
dict.fromkeys(a_list)
a_list = [1, 2, 3, ''a'', ''b'', ''c'']
dict.fromkeys(a_list)
{'''':无,1:无, 2:无,3:无,''c'':无,''b'':无}
-
Benji York
{''a'': None, 1: None, 2: None, 3: None, ''c'': None, ''b'': None}
--
Benji York
David Bear写道:
David Bear wrote:
是否有一种简单的方法可以创建一个字典对象,其成员
''alist''是字典中的键,键的值设置为
null?
Is there an easy way to create a dictionary object with the members of
''alist'' being the keys in the dictionary, and the value of the keys set to
null?
adict = dict.fromkeys(alist)
adict = dict.fromkeys(alist)
>
这篇关于列表中的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!