Python - 将多个字典值分配给一行中的不同变量

时间:2013-09-11 05:23:00

标签: python

我有以下代码(产生语法错误,顺便说一句)。有人可以帮我修复它,这样我就可以得到一个产生预期结果的版本吗?

al = [{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}, {'a': 7, 'b': 8, 'c': 9}, {'a': 10, 'b': 11, 'c': 12}]
a, b, c = [i.a, i.b, i.c for i in al]

预期结果:

a = [1, 4, 7, 10]
b = [2, 5, 8, 11]
c = [3, 6, 9, 12]

8 个答案:

答案 0 :(得分:4)

如果您事先知道钥匙:

>>> al = [{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}, {'a': 7, 'b': 8, 'c': 9}, {'a': 10, 'b': 11, 'c': 12}]
>>> a, b, c = [[d[key] for d in al] for key in 'abc'] # ('a', 'b', 'c')
>>> a
[1, 4, 7, 10]
>>> b
[2, 5, 8, 11]
>>> c
[3, 6, 9, 12]

如果您事先不知道密钥:

>>> d = {key: [d[key] for d in al] for key in al[0]}
>>> d
{'a': [1, 4, 7, 10], 'c': [3, 6, 9, 12], 'b': [2, 5, 8, 11]}
>>> a, b, c = map(d.get, 'abc') # OR map(d.get, ('a', 'b', 'c'))
>>> a
[1, 4, 7, 10]
>>> b
[2, 5, 8, 11]
>>> c
[3, 6, 9, 12]

答案 1 :(得分:1)

如果您的密钥未知,您只需转置数据并创建另一个转置字典,您只需通过keys访问该字典而不是创建独立变量

>>> al = [{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}, {'a': 7, 'b': 8, 'c': 9}, {'a': 10, 'b': 11, 'c': 12}]
>>> keys = al[0].keys()
>>> #Given your list of dictionary
>>> al = [{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}, {'a': 7, 'b': 8, 'c': 9}, {'a': 10, 'b': 11, 'c': 12}]
>>> #determine the keys
>>> keys = al[0].keys()
>>> #and using itemgetter
>>> from operator import itemgetter
>>> #create a transpose dictionary
>>> al_transpose = dict(zip(keys,zip(*map(itemgetter(*keys),al))))
>>> al_transpose['a']
(1, 4, 7, 10)
>>> al_transpose['b']
(2, 5, 8, 11)
>>> al_transpose['c']
(3, 6, 9, 12)

注意不推荐

如果您确实想要创建独立变量,可以通过将词典添加到locals

来实现。
locals().update(al_transpose)
>>> a
(1, 4, 7, 10)
>>> b
(2, 5, 8, 11)
>>> c
(3, 6, 9, 12)

答案 2 :(得分:0)

如果您对单行,感兴趣,并且如果您保证al 的词组中键的顺序和存在,那么:

>>> al = [{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}, {'a': 7, 'b': 8, 'c': 9}, {'a': 10, 'b': 11, 'c': 12}]
>>> dict(zip(al[0].keys(),zip(*[i.values() for i in al])))
{'a': (1, 4, 7, 10), 'c': (3, 6, 9, 12), 'b': (2, 5, 8, 11)}

答案 3 :(得分:0)

你可以这样做:

al = [{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}, {'a': 7, 'b': 8, 'c': 9}, {'a': 10, 'b': 11, 'c': 12}]

result={}
for dic in al:
    for key,val in dic.items():
        result.setdefault(key,[]).append(val)

print result     
# {'a': [1, 4, 7, 10], 'c': [3, 6, 9, 12], 'b': [2, 5, 8, 11]}
a,b,c=result['a'],result['b'],result['c']   

print a,b,c
# [1, 4, 7, 10] [2, 5, 8, 11] [3, 6, 9, 12]

如果要将字典中的名称绑定到当前名称空间中的名称,可以执行以下操作:

al = [{'a': 1, 'b': 2, 'c': 3, 'd':4}, 
      {'a': 4, 'b': 5, 'c': 6}, 
      {'a': 7, 'b': 8, 'c': 9}]

result={}
for dic in al:
    for k in dic:
        result.setdefault(k,[]).append(dic[k])

print result     
# {'a': [1, 4, 7, 10], 'c': [3, 6, 9, 12], 'b': [2, 5, 8, 11], 'd': [4]}

for k in result:
    code=compile('{}=result["{}"]'.format(k,k), '<string>', 'exec')
    exec code

print a,b,c,d  
# [1, 4, 7, 10] [2, 5, 8, 11] [3, 6, 9, 12] [4]

答案 4 :(得分:0)

试试这个

a,b,c = [map(lambda x:x[ele], al) for ele in 'abc']

答案 5 :(得分:0)

>>> al = [{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}, {'a': 7, 'b': 8, 'c': 9}, {'a': 10, 'b': 11, 'c': 12}]
>>> from operator import itemgetter
>>> a, b, c = zip(*map(itemgetter(*'abc'),(al)))
>>> a
(1, 4, 7, 10)
>>> b
(2, 5, 8, 11)
>>> c
(3, 6, 9, 12)

答案 6 :(得分:0)

>>> al = [{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}, {'a': 7, 'b': 8, 'c': 9}, {'a': 10, 'b': 11, 'c': 12}]
>>> a, b, c = zip(*(sorted(x.values(), key=x.get) for x in al))
>>> a
(1, 4, 7, 10)
>>> b
(3, 6, 9, 12)
>>> c
(2, 5, 8, 11)

答案 7 :(得分:0)

这是您需要修改方法的方法

>>> al = [{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}, {'a': 7, 'b': 8, 'c': 9}, {'a': 10, 'b': 11, 'c': 12}]
>>> a, b, c = zip(*((i['a'], i['b'], i['c']) for i in al))

首先,您需要使用i['a']代替i.a等,因为a是关键,而不是属性

这样会给你

>>> [(i['a'], i['b'], i['c']) for i in al]
[(1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12)]

啊,行和列是错误的方式。他们交换行和列的标准技巧是zip(*A)

>>> zip(*[(1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12)])
[(1, 4, 7, 10), (2, 5, 8, 11), (3, 6, 9, 12)]

将这两个想法结合起来,可以在这个答案的顶部给出表达

更直接的方法是嵌套列表理解

>>> a, b, c = [[x[k] for x in al] for k in 'abc']

如果键不是单个字符,则需要将它们作为元组缩写

写出来
>>> a, b, c = [[x[k] for x in al] for k in ('a', 'b', 'c')]