这两个字典都有菲奥娜图书馆的SeabFeFILE模式,它们最终打印出了它们的差异,我还需要提供哪一行属于哪一个字典:
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')}
您可以这样做:
输出:
相关问题 更多 >
编程相关推荐