Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.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 列表到字典转换的列表_Python - Fatal编程技术网

Python 列表到字典转换的列表

Python 列表到字典转换的列表,python,Python,我有一份清单: L = [['Bob'],['5', '0', '0', '0', '0'],['Jack'],['3', '0', '0', '1', '-3'],['Larry'],['1', '-3', '1', '-5', '0'], ...] 我想将此列表转换为一个字典,其中名称为键,数字为值,以便返回: {'Bob':[5, 0, 0, 0, 0], 'Jack':[3, 0, 0, 1, -3], 'Larry':[1, -3, 1, -5, 0], ...} 所以我想要一个字

我有一份清单:

L = [['Bob'],['5', '0', '0', '0', '0'],['Jack'],['3', '0', '0', '1', '-3'],['Larry'],['1', '-3', '1', '-5', '0'], ...]
我想将此列表转换为一个字典,其中名称为键,数字为值,以便返回:

{'Bob':[5, 0, 0, 0, 0], 'Jack':[3, 0, 0, 1, -3], 'Larry':[1, -3, 1, -5, 0], ...}
所以我想要一个字典,用字符串作为键,用整数数组作为每个字符串的值,但是,我不知道该怎么做


这里困扰我的主要问题是如何使字典将列表中的每个其他条目作为值,同时将字符串转换为整数。

您可以使用字典理解:

L = [['Bob'],['5', '0', '0', '0', '0'],['Jack'],['3', '0', '0', '1', '-3'],['Larry'],['1', '-3', '1', '-5', '0']]
new_l = {L[i][0]:list(map(int, L[i+1])) for i in range(0, len(L), 2)}
L = [['Bob'],['5', '0', '0', '0', '0'],['Jack'],['3', '0', '0', '1', '-3'],['Larry'],['1', '-3', '1', '-5', '0']]
new_l = {
L[i][0]: L[i+1] for i in range(0,len(L)-1, 2)
}
输出:

{'Bob': [5, 0, 0, 0, 0], 'Larry': [1, -3, 1, -5, 0], 'Jack': [3, 0, 0, 1, -3]}
{'Bob': [5, 0, 0, 0, 0], 'Larry': [1, -3, 1, -5, 0], 'Jack': [3, 0, 0, 1, -3]}
{'Bob': [5, 0, 0, 0, 0], 'Larry': [1, -3, 1, -5, 0], 'Jack': [3, 0, 0, 1, -3]}

您可以使用字典理解:

L = [['Bob'],['5', '0', '0', '0', '0'],['Jack'],['3', '0', '0', '1', '-3'],['Larry'],['1', '-3', '1', '-5', '0']]
new_l = {L[i][0]:list(map(int, L[i+1])) for i in range(0, len(L), 2)}
L = [['Bob'],['5', '0', '0', '0', '0'],['Jack'],['3', '0', '0', '1', '-3'],['Larry'],['1', '-3', '1', '-5', '0']]
new_l = {
L[i][0]: L[i+1] for i in range(0,len(L)-1, 2)
}
输出:

{'Bob': [5, 0, 0, 0, 0], 'Larry': [1, -3, 1, -5, 0], 'Jack': [3, 0, 0, 1, -3]}
{'Bob': [5, 0, 0, 0, 0], 'Larry': [1, -3, 1, -5, 0], 'Jack': [3, 0, 0, 1, -3]}
{'Bob': [5, 0, 0, 0, 0], 'Larry': [1, -3, 1, -5, 0], 'Jack': [3, 0, 0, 1, -3]}

更新:

dict_from_list = {key[0]: value for key, [int(elem) for elem in value] in zip(L[::2], L[1::2])}
我没有注意到值列表中的单个元素是str。将它们转换为int类型,是我在这里发布的所有解决方案中最好的、最干净的和pythonic的解决方案。我建议您详细研究它,并将其纳入python编程中,这是理所当然的。
如果您不想使用
map
,那么在dict comprehension中使用列表理解是一种方法,如上面的代码所示

原始答案:
dict\u from\u list={key[0]:key的值,zip中的值(L[::2],L[1::2])

这里有一个替代方法

L = [['Bob'],['5', '0', '0', '0', '0'],['Jack'],['3', '0', '0', '1', '-3'],['Larry'],['1', '-3', '1', '-5', '0']]
{k[0]: list(map(int, v)) for k, v in zip(L[::2], L[1::2])}
输出:

{'Bob': [5, 0, 0, 0, 0], 'Larry': [1, -3, 1, -5, 0], 'Jack': [3, 0, 0, 1, -3]}
{'Bob': [5, 0, 0, 0, 0], 'Larry': [1, -3, 1, -5, 0], 'Jack': [3, 0, 0, 1, -3]}
{'Bob': [5, 0, 0, 0, 0], 'Larry': [1, -3, 1, -5, 0], 'Jack': [3, 0, 0, 1, -3]}

在手机上,你可以检查一下,但你明白了。是的,谢谢你的回复:)这确实有效。非常感谢。欢迎来到SO。不幸的是,这不是一个讨论论坛或教程。请花点时间阅读该页面上的链接和其他链接。花些时间练习这些例子。它会让你了解Python提供的帮助你解决问题的工具。@wwii I一定会仔细阅读。谢谢你的提示,很抱歉给你带来麻烦。查阅字典理解、列表切片、压缩以获取更多信息。我想你用的是蟒蛇3。