Ale*_*lex 1 python dictionary list
for x in names:
dictionary = {x : [], }
Run Code Online (Sandbox Code Playgroud)
我希望我的字典有一个包含每个名称元素的空列表,但我只得到最后一个(显然是最重要的)。在列表中我们可以使用 .append(). 我如何用字典做到这一点?
编辑:假设名字有吉米和亚历克斯。
dictionary = {Jimmy : [], Alex : []}
Run Code Online (Sandbox Code Playgroud)
不要每次都创建一个新的字典,而是将名称添加到同一个字典中。
dictionary = {}
for x in names:
dictionary[x] = []
Run Code Online (Sandbox Code Playgroud)
或者,简称:
dictionary = {x: [] for x in names}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7611 次 |
| 最近记录: |