Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python dict中用一个键添加多个贵重物品?_Python_Python 3.x_Dictionary_Zip - Fatal编程技术网

如何在python dict中用一个键添加多个贵重物品?

如何在python dict中用一个键添加多个贵重物品?,python,python-3.x,dictionary,zip,Python,Python 3.x,Dictionary,Zip,我有一个名字列表,我正试图将它们添加到名为“name”的键下 list_of_names = ['john', 'lisa', 'david', 'frans'] key = ['name'] 与: 代码返回: [{'name': 'j'}, {'name': 'l'}, {'name': 'd'}, {'name': 'f'}] 那么,如何添加全名而不仅仅是第一个字母呢?这里不要使用zip;只需使用dict文本: key = 'name' namelist = [{key: i} for

我有一个名字列表,我正试图将它们添加到名为“name”的键下

list_of_names = ['john', 'lisa', 'david', 'frans']
key = ['name']
与:

代码返回:

[{'name': 'j'}, {'name': 'l'}, {'name': 'd'}, {'name': 'f'}] 
那么,如何添加全名而不仅仅是第一个字母呢?

这里不要使用zip;只需使用dict文本:

key = 'name'
namelist = [{key: i} for i in list_of_names]
或者直接使用:

namelist = [{'name': i} for i in list_of_names]
zip在两个输入上循环以配对每个元素;比如说,将['name']与john配对,只会产生一个配对,即名字中的第一个字符。如果您必须使用zip,那么至少也要将i放在列表中:

namelist = [dict(zip(key, [i])) for i in list_of_names]
namelist = [dict(zip(key, [i])) for i in list_of_names]