1

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

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]
4

8 回答 8

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]
于 2013-09-11T05:28:11.293 回答
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)
于 2013-09-11T06:08:50.947 回答
0

如果您对单线感兴趣,并且如果您保证 dicts 中键的顺序和存在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)}
于 2013-09-11T05:32:52.933 回答
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]
于 2013-09-11T05:33:37.080 回答
0

尝试这个

a,b,c = [map(lambda x:x[ele], al) for ele in 'abc']
于 2013-09-11T05:47:08.330 回答
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)
于 2013-09-11T06:03:05.687 回答
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)
于 2013-09-11T06:19:28.177 回答
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')]
于 2013-09-11T06:22:55.457 回答