0
list = ['Roses are red','Sun is shining','cream comes from the bottom']

dict = {'john':'Roses are red', 'john':'Sun is shining','john':'cream comes from the bottom'}

我希望我说得通。

4

4 回答 4

6

你不......键在字典中必须是唯一的......

你可以做一个听写列表

[ {'john':'roses'},{'john':'roses2'},...]
#like this
[ {'john':itm} for itm in my_list ]

或元组/列表列表

[ ('john':'roses'),('john':'roses2'),...]
#like this
[ ('john',itm) for itm in my_list ]

或带有附加到 john 的列表的字典

{'john':[1,2,3]}
#like this
{'john':my_list }
于 2012-10-09T17:03:11.810 回答
0

即使你可以在病房之后使用字典,你也不能使用它。如果你真的想要这样的东西,你可以使用 touples 列表或这样的列表列表

[('john','Roses are red'), ('john','Sun is shining'),('john','cream comes from the bottom')]
于 2012-10-09T17:05:58.893 回答
0

from the docs:'

It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary)

于 2012-10-09T17:06:06.953 回答
0

如前所述,键必须是唯一的......但是,您可以使用列表作为键:

>>> list1 = ['Roses are red','Sun is shining','cream comes from the bottom']
>>> dict1 = { x:'john' for x in list1 }
>>> dict1
>>> {'cream comes from the bottom': 'john', 'Sun is shining': 'john', 'Roses are red': 'john'}
于 2012-10-09T17:21:00.067 回答