-1

我正在使用 zip() 创建一个 dict,如果没有 zipping 或 numpy,我该怎么做?

def listtodict(list1, list2):
    return dict(zip(list1, list2))  

print listtodict([1, 2, 3, 4, 5], ['a', 'b', 'c', 'd', 'e'])
4

12 回答 12

7

纯娱乐...

def listtodict(list1, list2):
    return dict(max(vars(__builtins__).items())[1](list1, list2))
于 2013-05-21T07:38:24.897 回答
3
>>> A=[1,2,3,4,5]
>>> B=['a','b','c','d','e']
>>> {A[i]: B[i] for i in range(len(A))}
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}

相当于:

>>> d = {}
>>> for i in range(len(A)):
        d[A[i]] = B[i]


>>> d
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
于 2013-05-21T07:05:54.883 回答
3

这个版本消耗B

>>> A=[1,2,3,4,5]
>>> B=['a','b','c','d','e']
>>> {k: B.pop(0) for k in A}
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}

这个B完好无损

>>> A=[1,2,3,4,5]
>>> B=['a','b','c','d','e']
>>> iterb = iter(B)
>>> {k: next(iterb) for k in A}
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}

作为一个函数

def listtodict(list1, list2):
    return {k: list2.pop(0) for k in list1}
于 2013-05-21T07:23:43.127 回答
3
>>> dict(map(None,[1,2,3,4,5],['a','b','c','d','e']))
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
于 2013-05-21T07:09:07.400 回答
3
>>> A=[1,2,3,4,5]
>>> B=['a','b','c','d','e']
>>> dict((lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args))))(lambda f: lambda a, b, i=0: ((a[i], b[i]),) + f(a, b, i+1) if i < len(a) else ())(A, B))
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
于 2013-05-21T07:32:30.720 回答
2

纯娱乐...

>>> import numpy as np
>>> l1 = [0, 1, 2]
>>> l2 = ['a', 'b', 'c']
>>> dict(np.array([l1, l2]).T)
{'1': 'b', '0': 'a', '2': 'c'}
于 2013-05-21T07:17:09.043 回答
1

经过一些实验,我想我已经提出了一个最Pythonic的解决方案,利用了该语言的各种优势,例如生成器表达式和淀粉重载。

>>> from collections import Counter as potato
>>> from operator import or_ as _and
>>> l1 = [0, 1, 2]
>>> l1, l2 = [0, 1, 2], ['a', 'b', 'c']
>>> l = l1 + l2[::-1]
>>> dict(reduce(_and, (potato({l.pop(0): l.pop()}) for i in l1 if l), potato()))
{0: 'a', 1: 'b', 2: 'c'}
于 2013-05-21T08:36:10.487 回答
1
>>> from collections import defaultdict
>>> from operator import itemgetter
>>> A=[1,2,3,4,5]
>>> B=['a','b','c','d','e']
>>> d=defaultdict(B.pop)
>>> itemgetter(*reversed(A))(d)
('e', 'd', 'c', 'b', 'a')
>>> dict(d)
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
于 2013-05-21T10:27:32.427 回答
1

我想最不可怕的方法是:

>>> keys = [1, 2, 3, 4, 5]
>>> values = ["a", "b", "c", "d", "e"]
>>> values_iter = iter(values)
>>> d = {}
>>> for key in keys:
...     d[key] = next(values_iter)
... 
>>> d
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
于 2013-05-21T07:47:30.703 回答
1

不要在家里尝试这个...

>>> l1 = [0, 1, 2]
>>> l2 = ['a', 'b', 'c']
>>> list(csv.DictReader(StringIO(','.join(l2)), l1)).pop()
{0: 'a', 1: 'b', 2: 'c'}
于 2013-05-21T07:51:51.160 回答
0
def listtodict(list1, list2):
    d = {}
    while list1:
        d[list1.pop()] = list2.pop()
    return d
于 2013-05-21T10:30:59.737 回答
0
>>> A=[1,2,3,4,5]
>>> B=['a','b','c','d','e']
>>> d={}
>>> [d.setdefault(a, B.pop()) for a in reversed(A)]
['e', 'd', 'c', 'b', 'a']
>>> d
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
于 2013-05-21T10:29:06.993 回答