python中2个列表组合生成对应的字典

1、构建字典的 2 个列表相同

>>> a = [1,2,3,4]
>>> b = ['ab','ac','ad']
>>> dict(zip(a,b))
{1: 'ab', 2: 'ac', 3: 'ad'}
>>>

2、构建字典的 2 个列表不同(key比value多)

>>> a = [1,2,3,4]
>>> c = ['aa','ss']
>>> dict(zip(a,c))
{1: 'aa', 2: 'ss'}
>>>

3、构建字典的 2 个列表不同(key比value少)

>>> a = [1,2,3,4]
>>> d = ['fa','fb','fc','fd','fe']
>>> dict(zip(a,d))
{1: 'fa', 2: 'fb', 3: 'fc', 4: 'fd'}
>>>

posted @ 2016-01-19 21:05  芳草天涯  阅读(17327)  评论(0编辑  收藏  举报