Python 字典-如何映射键列表和值列表?

Python 字典-如何映射键列表和值列表?,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,我有两个不同的清单: key = ['key1', 'key2', 'key3'] value = ['value1', 'value2', 'value3'] 我想这样映射它: dictionary = {'key1':'value1', 'key2':'value2', 'key3':'value3'} 我试过这个: dictionary = {key, value} 但我得到了这个结果: TypeError: dict expe

我有两个不同的清单:

key = ['key1', 'key2', 'key3']
value = ['value1', 'value2', 'value3']
我想这样映射它:

dictionary = {'key1':'value1',
              'key2':'value2',
              'key3':'value3'}
我试过这个:

dictionary = {key, value}
但我得到了这个结果:

TypeError: dict expected at most 1 arguments, got 2


            

执行此操作以获得所需的结果

dictionary = dict(zip(key, value))
试试这个:

dictionary = {key[i]:value[i] for i in range(len (key))}
这就是所谓的词典理解

基本上,您所做的是在索引中循环 以及访问字典中的各种值


可能有帮助

使用
zip
是保留列表中键值项顺序的好方法。请在询问以下问题之前搜索堆栈溢出:
dictionary = {key[i]:value[i] for i in range(len (key))}