执行以下代码后,我有一个csv文件,如下所示:
with open('XYZ.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row)
输出:
OrderedDict([('', '0'), ('img_id', '0359a'), ('f1', '2'), ('f2', '1'), ('f3', '1'),
('f4', '0'), ('f5', '2'), ('f6', '2'), ('f7', '0'), ('f8', '2'), ('f9', '2')])
OrderedDict([('', '1'), ('img_id', '0577a'), ('f1', '2'), ('f2', '1'), ('f3', '1'),
('f4', '0'), ('f5', '2'), ('f6', '2'), ('f7', '0'), ('f8', '1'), ('f9', '2')])
OrderedDict([('', '2'), ('img_id', '1120a'), ('f1', '2'), ('f2', '1'), ('f3', '1'),
('f4', '3'), ('f5', '2'), ('f6', '2'), ('f7', '0'), ('f8', '2'), ('f9', '2')])
如何创建如下所示的字典:
{
'0359a': ('2', '1', '1', '0', '2', '2', '0', '2', '2'),
'0577a': ('2', '1', '1', '0', '2', '2', '0', '1', '2'),
'1120a': ('2', '1', '1', '3', '2', '2', '0', '2', '2')
}
我的代码是:
d = {}
with open('XYZ.csv') as csvfile:
reader = csv.DictReader(csvfile)
for i in reader:
for j in i.keys():
if j in cols:
d[i['img_id']] = i[j]
print(d)
这让我屈服:
{'0359a': '2', '0577a': '2', '1120a': '2'}
如何避免这种覆盖?
答案 0 :(得分:1)
这可以通过以下简单的字典理解来实现(请参阅注释中的解释):
lines = [
OrderedDict([('', '0'), ('img_id', '0359a'), ('f1', '2'), ('f2', '1'), ('f3', '1'),
('f4', '0'), ('f5', '2'), ('f6', '2'), ('f7', '0'), ('f8', '2'), ('f9', '2')]),
OrderedDict([('', '1'), ('img_id', '0577a'), ('f1', '2'), ('f2', '1'), ('f3', '1'),
('f4', '0'), ('f5', '2'), ('f6', '2'), ('f7', '0'), ('f8', '1'), ('f9', '2')]),
OrderedDict([('', '2'), ('img_id', '1120a'), ('f1', '2'), ('f2', '1'), ('f3', '1'),
('f4', '3'), ('f5', '2'), ('f6', '2'), ('f7', '0'), ('f8', '2'), ('f9', '2')])]
# for our new dictionary
# key is img_id value in OrderedDict
# and value is a list of all values in OrderedDict if their key isn't '' or 'img_id'
d = {l['img_id']: tuple([v for k, v in l.items() if k not in ('', 'img_id')]) for l in lines}
print(d)
这给我们:
{'0359a': ('2', '1', '1', '0', '2', '2', '0', '2', '2'),
'1120a': ('2', '1', '1', '3', '2', '2', '0', '2', '2'),
'0577a': ('2', '1', '1', '0', '2', '2', '0', '1', '2')}
答案 1 :(得分:0)
您可以使用 defaultdict ,每个键都是d [i ['img_id']],值是您要附加的列表
from collections import defaultdict
d = defaultdict(list)
...
d[i['img_id']].append(i[j])
答案 2 :(得分:0)
您可以使用以下dict理解来解压缩dict项中的键和值:
{k: tuple(i for _, i in v) for _, (_, k), *v in (d.items() for d in reader)}
这将返回:
{'0359a': ('2', '1', '1', '0', '2', '2', '0', '2', '2'), '0577a': ('2', '1', '1', '0', '2', '2', '0', '1', '2'), '1120a': ('2', '1', '1', '3', '2', '2', '0', '2', '2')}