Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.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_List_Dictionary - Fatal编程技术网

Python 使用列表中的键和另一个列表中的值作为列表创建字典

Python 使用列表中的键和另一个列表中的值作为列表创建字典,python,list,dictionary,Python,List,Dictionary,我有一张单子 key_list = ['m.title', 'm.studio', 'm.gross', 'm.year'] cols = [ ['Titanic', 'The Lord of the Rings: The Return of the King', 'Toy Story 3'], ['Par.', 'NL', 'BV'], ['2186.8', '1119.9', '1063.2'], ['1997', '2003', '2010'] ] 我想

我有一张单子

key_list = ['m.title', 'm.studio', 'm.gross', 'm.year']
cols = [
    ['Titanic', 'The Lord of the Rings: The Return of the King', 'Toy Story 3'], 
    ['Par.', 'NL', 'BV'],
    ['2186.8', '1119.9', '1063.2'],
    ['1997', '2003', '2010']
]
我想构造一个字典表,它的键是键列表的元素,值是相应的cols子列表

我目前的代码如下:

i = 0
for key in key_list:
    table_dict[key] = cols[i]
    i = i + 1

return table_dict
我似乎找不到错误,但当我运行它时,我得到:

dict[key] = cols[i]
IndexError: list index out of range
您只需输入键和值并将其传递给
dict
。您可以阅读更多有关构建词典的内容

输出

{'m.gross': ['2186.8', '1119.9', '1063.2'], 'm.studio': ['Par.', 'NL', 'BV'], 'm.year': ['1997', '2003', '2010'], 'm.title': ['Titanic', 'The Lord of the Rings: The Return of the King', 'Toy Story 3']}
{'m.gross': ['2186.8', '1119.9', '1063.2'], 'm.studio': ['Par.', 'NL', 'BV'], 'm.year': ['1997', '2003', '2010'], 'm.title': ['Titanic', 'The Lord of the Rings: The Return of the King', 'Toy Story 3']}
您只需输入键和值并将其传递给
dict
。您可以阅读更多有关构建词典的内容

输出

{'m.gross': ['2186.8', '1119.9', '1063.2'], 'm.studio': ['Par.', 'NL', 'BV'], 'm.year': ['1997', '2003', '2010'], 'm.title': ['Titanic', 'The Lord of the Rings: The Return of the King', 'Toy Story 3']}
{'m.gross': ['2186.8', '1119.9', '1063.2'], 'm.studio': ['Par.', 'NL', 'BV'], 'm.year': ['1997', '2003', '2010'], 'm.title': ['Titanic', 'The Lord of the Rings: The Return of the King', 'Toy Story 3']}

您提供的示例可以正常工作。代码中可能还有另一个问题。但是,错误消息告诉您的是

列表cols的索引i超出范围。这意味着在迭代第一个列表(其中有4个元素,因此迭代4次)时,其他列表列没有足够的项-这意味着可能少于4个

围绕这个问题的工作涉及python文档

输出:


您提供的示例可以正常工作。代码中可能还有另一个问题。但是,错误消息告诉您的是

列表cols的索引i超出范围。这意味着在迭代第一个列表(其中有4个元素,因此迭代4次)时,其他列表列没有足够的项-这意味着可能少于4个

围绕这个问题的工作涉及python文档

输出:

如果您想要这样的输出

{'m.gross': 'Toy Story 3', 'm.studio': 'The Lord of the Rings: The Return of the King','m.title': 'Titanic'}{'m.gross': 'BV', 'm.studio': 'NL', 'm.title': 'Par.'}{'m.gross': '1063.2', 'm.studio': '1119.9', 'm.title': '2186.8'}{'m.gross': '2010', 'm.studio': '2003','m.title': '1997'}
如果您想要这样的输出

{'m.gross': 'Toy Story 3', 'm.studio': 'The Lord of the Rings: The Return of the King','m.title': 'Titanic'}{'m.gross': 'BV', 'm.studio': 'NL', 'm.title': 'Par.'}{'m.gross': '1063.2', 'm.studio': '1119.9', 'm.title': '2186.8'}{'m.gross': '2010', 'm.studio': '2003','m.title': '1997'}

请给我们看完整的stacktrace。我想您在某处将表\u dict初始化为表\u dict=dict()?@AndreiBoyanov啊,就是这样,谢谢!请给我们看完整的stacktrace。我想您在某处将表\u dict初始化为表\u dict=dict()?@AndreiBoyanov啊,就是这样,谢谢!同意,尽管我可能会抛出一个断言len(key_list)==len(cols)。同意,尽管我可能会抛出一个断言len(key_list)==len(cols)。