我有一个列表,如:
a = [
['A', 'America', 'LA', '1123', '2014-05-01', [('A', '211'), ('AD', '398')]],
['D', 'America', 'LA', '1135', '2014-05-01', [('A', '211'), ('AD', '398')]],
['I', 'America', 'SF', '1145', '2014-05-01', [('A', '211'), ('AD', '398')]],
['A', 'England', 'LND', '3564', '2014-05-01', [('A', '211'), ('AD', '398')]],
['D', 'Dubai', 'DUB', '9990', '2014-05-01', [('A', '211'), ('AD', '398')]],
['D', 'Dubai', 'DUX', '9670', '2014-05-01', [('A', '211'), ('AD', '398')]],
['I', 'Dubai', 'DUB', '9800', '2014-05-01', [('A', '211'), ('AD', '398')]],
]
我想要一个嵌套的Dict,如:
d = {
'America': {
'LA': {
'1123' : ['A', '2014-05-01', [('A', '211'), ('AD', '398')]],
'1135': ['D', '2014-05-01', [('A', '211'), ('AD', '398')]]
},
'SF': {
'1145': ['I', '2014-05-01', [('A', '211'), ('AD', '398')]]
}
},
'England': {
'LND': {
'3564': ['A', '2014-05-01', [('A', '211'), ('AD', '398')]]
}
},
'Dubai': {
'DUB': {
'9990': ['D', '2014-05-01', [('A', '211'), ('AD', '398')]],
'9800': ['I', '2014-05-01', [('A', '211'), ('AD', '398')]]
},
'DUX': {
'9670': ['D', '2014-05-01', [('A', '211'), ('AD', '398')]]
}
}
}
我已经尝试过,但仍然无法做到,请检查我的代码并帮助我!
pd = defaultdict(dict)
for row in a:
country = defaultdict(dict)
for c in a:
dest = defaultdict(list)
for d in a:
if c[2] == d[2]:
dest[d[3]].append([d[1], d[0],d[4],d[5],d[6], d[7]])
else:
continue
country[c[2]] = dest
pd[row[1]] = country
答案 0 :(得分:3)
from operator import itemgetter
from itertools import groupby
getter, d = itemgetter(0, 4, 5), {}
for key, grp in groupby(sorted(a, key=lambda x: x[3]), key=lambda x: x[3]):
for item in grp:
d.setdefault(item[1], {}).setdefault(
item[2], {})[item[3]] = list(getter(item))
print d
<强>输出强>
{'America': {'LA': {'1123': ['A',
'2014-05-01',
[('A', '211'), ('AD', '398')]],
'1135': ['D',
'2014-05-01',
[('A', '211'), ('AD', '398')]]},
'SF': {'1145': ['I',
'2014-05-01',
[('A', '211'), ('AD', '398')]]}},
'Dubai': {'DUB': {'9800': ['I',
'2014-05-01',
[('A', '211'), ('AD', '398')]],
'9990': ['D',
'2014-05-01',
[('A', '211'), ('AD', '398')]]},
'DUX': {'9670': ['D',
'2014-05-01',
[('A', '211'), ('AD', '398')]]}},
'England': {'LND': {'3564': ['A',
'2014-05-01',
[('A', '211'), ('AD', '398')]]}}}
答案 1 :(得分:2)
尝试以下方法:
pd = dict()
for (k4, k1, k2, k3, k5, k6) in a:
pd.setdefault(k1, dict())
pd[k1].setdefault(k2, dict())
pd[k1][k2].setdefault(k3, dict())
pd[k1][k2][k3] = [k4, k5, k6]
输出结果为:
{'America': {'LA': {'1123': ['A', '2014-05-01', [('A', '211'), ('AD', '398')]],
'1135': ['D', '2014-05-01', [('A', '211'), ('AD', '398')]]},
'SF': {'1145': ['I', '2014-05-01', [('A', '211'), ('AD', '398')]]}},
'Dubai': {'DUB': {'9800': ['I', '2014-05-01', [('A', '211'), ('AD', '398')]],
'9990': ['D', '2014-05-01', [('A', '211'), ('AD', '398')]]},
'DUX': {'9670': ['D', '2014-05-01', [('A', '211'), ('AD', '398')]]}},
'England': {'LND': {'3564': ['A',
'2014-05-01',
[('A', '211'), ('AD', '398')]]}}}
答案 2 :(得分:2)
使用嵌套的defaultdicts
,然后您可以在{-1}上的for循环中使用简单的赋值语句。
a
<强>输出:强>
from collections import defaultdict
out = defaultdict(lambda : defaultdict(lambda :defaultdict(dict)))
for item in a:
out[item[1]][item[2]][item[3]] = [item[0]] + item[4:]
print out == d # Comparing it to the fixed expected output.
print out
使用True
defaultdict(<function <lambda> at 0xadcd70>,
{'Dubai': defaultdict(<function <lambda> at 0x106a398>,
{'DUX': defaultdict(<type 'dict'>,
{'9670': ['D', '2014-05-01', [('A', '211'), ('AD', '398')]]}),
'DUB': defaultdict(<type 'dict'>,
{'9990': ['D', '2014-05-01', [('A', '211'), ('AD', '398')]],
'9800': ['I', '2014-05-01', [('A', '211'), ('AD', '398')]]})}),
'America': defaultdict(<function <lambda> at 0x1066c08>,
{'SF': defaultdict(<type 'dict'>,
{'1145': ['I', '2014-05-01', [('A', '211'), ('AD', '398')]]}),
'LA': defaultdict(<type 'dict'>,
{'1123': ['A', '2014-05-01', [('A', '211'), ('AD', '398')]],
'1135': ['D', '2014-05-01', [('A', '211'), ('AD', '398')]]})}),
'England': defaultdict(<function <lambda> at 0x106a320>,
{'LND': defaultdict(<type 'dict'>,
{'3564': ['A', '2014-05-01', [('A', '211'), ('AD', '398')]]})})})
和reduce()
:
operator.itemgetter
答案 3 :(得分:0)
您的代码提供:
Traceback (most recent call last):
File "<pyshell#12>", line 7, in <module>
dest[d[3]].append([d[1], d[0],d[4],d[5],d[6], d[7]])
IndexError: list index out of range
为什么呢?因为d
是例如:
['A', 'England', 'LND', '3564', '2014-05-01', [('A', '211'), ('AD', '398')]]
#0 1 2 3 4 5
因此d[6]
和d[7]
不存在。
最小修正是:
dest[d[3]].append([d[1], d[0], d[4], d[5]])
答案 4 :(得分:0)
这真令人困惑......
from itertools import groupby
f1 = lambda x: x[1]
f2 = lambda x: x[2]
f3 = lambda x: x[3]
b = sorted(a, key=lambda x: (x[3], x[2], x[1]))
d = {}
for i in groupby(b, f1):
d[i[0]] = {}
for j in groupby(i[1], f2):
d[i[0]][j[0]] = {}
for k in groupby(j[1], f3):
d[i[0]][j[0]][k[0]] = []
for l in k[1]:
d[i[0]][j[0]][k[0]].append(list(l)[0])
d[i[0]][j[0]][k[0]].append(list(l)[4])
d[i[0]][j[0]][k[0]].append(list(l)[5])
输出:
>>> d
{'America': {'LA': {'1123': ['A', '2014-05-01', [('A', '211'), ('AD', '398')]],
'1135': ['D', '2014-05-01', [('A', '211'), ('AD', '398')]]},
'SF': {'1145': ['I', '2014-05-01', [('A', '211'), ('AD', '398')]]}},
'Dubai': {'DUB': {'9800': ['I', '2014-05-01', [('A', '211'), ('AD', '398')]],
'9990': ['D', '2014-05-01', [('A', '211'), ('AD', '398')]]},
'DUX': {'9670': ['D', '2014-05-01', [('A', '211'), ('AD', '398')]]}},
'England': {'LND': {'3564': ['A',
'2014-05-01',
[('A', '211'), ('AD', '398')]]}}}