有两个带有Fiona库中shapefile模式的字典,最终会打印出它们的差异,我还需要提供哪一行属于哪个字典:
from collections import OrderedDict
from pprint import pprint
>>> d1 = {'properties': OrderedDict([('KAEK', 'str:12'),
... ('PROP_TYPE', 'str:4'),
... ('ORI_TYPE', 'int:1'),
... ('ORI_CODE', 'str:100'),
... ('DEC_ID', 'str:254'),
... ('ADDRESS', 'str:254'),
... ('NUM', 'str:9'),
... ('LEN', 'float:19.11'),
... ('AREA', 'float:19.11')]),
... 'geometry': 'Polygon'}
>>> d1 = d1['properties']
>>> d2 = {'properties': OrderedDict([('OBJECTID_1', 'int:9'),
... ('OBJECTID', 'int:9'),
... ('FID_PERIVL', 'int:9'),
... ('DESC_', 'str:254'),
... ('PROP_TYPE', 'str:4'),
... ('Shape_Leng', 'float:19.11'),
... ('Shape_Le_1', 'float:19.11'),
... ('Shape_Area', 'float:19.11'),
... ('PARCEL_COD', 'str:254'),
... ('KAEK', 'str:50'),
... ('NUM', 'int:4'),
... ('DEC_ID', 'int:4'),
... ('ADDRESS', 'int:4'),
... ('ORI_CODE', 'int:4'),
... ('ORI_TYPE', 'int:4')]),
... 'geometry': 'Polygon'}
>>> d2 = d2['properties']
我希望结果能显示出哪个字典起源于最后一部分中列出的对称差异:查看注释以查看我打算做什么。
pprint(set.symmetric_difference(set(d1.items()), set(d2.items())))
{('ADDRESS', 'int:4'),#somehow inform that this belongs to d2 and so on...
('ADDRESS', 'str:254'),#d1
('AREA', 'float:19.11'),#d1
('DEC_ID', 'int:4'),#d2
('DEC_ID', 'str:254'),
('DESC_', 'str:254'),
('FID_PERIVL', 'int:9'),
('KAEK', 'str:12'),
('KAEK', 'str:50'),
('LEN', 'float:19.11'),
('NUM', 'int:4'),
('NUM', 'str:9'),
('OBJECTID', 'int:9'),
('OBJECTID_1', 'int:9'),
('ORI_CODE', 'int:4'),
('ORI_CODE', 'str:100'),
('ORI_TYPE', 'int:1'),
('ORI_TYPE', 'int:4'),
('PARCEL_COD', 'str:254'),
('Shape_Area', 'float:19.11'),
('Shape_Le_1', 'float:19.11'),
('Shape_Leng', 'float:19.11')}
简单地,按字典名称对它们进行分类。我该怎么办?
尝试过:
pprint(set(('d1', el) if el in d1.items() else ('d2', el) for el in d2.items()))
但未正确返回:
{('d1', ('PROP_TYPE', 'str:4')),
('d2', ('ADDRESS', 'int:4')),
('d2', ('DEC_ID', 'int:4')),
('d2', ('DESC_', 'str:254')),
('d2', ('FID_PERIVL', 'int:9')),
('d2', ('KAEK', 'str:50')),
('d2', ('NUM', 'int:4')),
('d2', ('OBJECTID', 'int:9')),
('d2', ('OBJECTID_1', 'int:9')),
('d2', ('ORI_CODE', 'int:4')),
('d2', ('ORI_TYPE', 'int:4')),
('d2', ('PARCEL_COD', 'str:254')),
('d2', ('Shape_Area', 'float:19.11')),
('d2', ('Shape_Le_1', 'float:19.11')),
('d2', ('Shape_Leng', 'float:19.11'))}
正确的方法是像之前的最后一个一样显示它,但也要具有d1和d2信息。按此顺序:
pprint(set.symmetric_difference(set(d1.items()), set(d2.items())))
{('ADDRESS', 'int:4'),#somehow inform that this belongs to d2 and so on...
('ADDRESS', 'str:254'),#d1
('AREA', 'float:19.11'),#d1
('DEC_ID', 'int:4'),#d2
('DEC_ID', 'str:254'),
('DESC_', 'str:254'),
('FID_PERIVL', 'int:9'),
('KAEK', 'str:12'),
('KAEK', 'str:50'),
('LEN', 'float:19.11'),
('NUM', 'int:4'),
('NUM', 'str:9'),
('OBJECTID', 'int:9'),
('OBJECTID_1', 'int:9'),
('ORI_CODE', 'int:4'),
('ORI_CODE', 'str:100'),
('ORI_TYPE', 'int:1'),
('ORI_TYPE', 'int:4'),
('PARCEL_COD', 'str:254'),
('Shape_Area', 'float:19.11'),
('Shape_Le_1', 'float:19.11'),
('Shape_Leng', 'float:19.11')}
答案 0 :(得分:2)
您可以执行以下操作:
d1_items = set(d1.items())
d2_items = set(d2.items())
result = sorted([(k, 'd1', v) for k, v in d1_items if (k, v) not in d2_items] +
[(k, 'd2', v) for k, v in d2_items if (k, v) not in d1_items])
result = [(k, v, d) for k, d, v in result]
pprint(result)
输出:
[('ADDRESS', 'str:254', 'd1'),
('ADDRESS', 'int:4', 'd2'),
('AREA', 'float:19.11', 'd1'),
('DEC_ID', 'str:254', 'd1'),
('DEC_ID', 'int:4', 'd2'),
('DESC_', 'str:254', 'd2'),
('FID_PERIVL', 'int:9', 'd2'),
('KAEK', 'str:12', 'd1'),
('KAEK', 'str:50', 'd2'),
('LEN', 'float:19.11', 'd1'),
('NUM', 'str:9', 'd1'),
('NUM', 'int:4', 'd2'),
('OBJECTID', 'int:9', 'd2'),
('OBJECTID_1', 'int:9', 'd2'),
('ORI_CODE', 'str:100', 'd1'),
('ORI_CODE', 'int:4', 'd2'),
('ORI_TYPE', 'int:1', 'd1'),
('ORI_TYPE', 'int:4', 'd2'),
('PARCEL_COD', 'str:254', 'd2'),
('Shape_Area', 'float:19.11', 'd2'),
('Shape_Le_1', 'float:19.11', 'd2'),
('Shape_Leng', 'float:19.11', 'd2')]