按值范围划分字典的交集
Intersection of dictionaries by value ranges
我有一本字典,它们的形式是:
"id_1" : {
"_id" : ObjectId("5cefddaae40ed94ea713f7c8"),
"derived_from" : ObjectId("5cefddaae40ed94ea713f7c7"),
"epsg" : "32723",
"active" : True,
"name" : "DPA-Time",
"range_time_depth" : [ 4648.91338, 5641.545770000001 ],
"type" : "Polygon",
"iline": 6998
}
我有一本像数据库查询一样工作的字典:
{
'_id': { '$in': ['5cefddaae40ed94ea713f7c8','5cefddaae40ed94ea713f7c8'] },
'iline': {'$gte': 6998, '$lte': 7000}
}
所以,我想检索词典字典中所有 _id
等于 '5cefddaae40ed94ea713f7c8'
或 '5cefddaae40ed94ea713f7c8'
和 'iline'
在 6998 和 7000 之间的词典。
我知道交集方法有助于匹配字典值,但是当我想将值与值的 range 匹配时,我该怎么做?
我做了什么:
def check_correspondence(obj, query):
for _, value in obj.items():
if value.keys() & query.keys():
return True
else: return False
我循环遍历提到的字典并应用 & 运算符。但是,此运算符似乎不适用于范围比较,只能用于严格相等。
当我尝试时,我收到 TypeError: unhashable type: 'list'
您不需要检查所有密钥,因为您只想检查 ID 和 iline
。
注意,&
和 and
在 python 中是不一样的。 &
是 bit-wise operator.
objs = {
"id_1" : {
"_id" : "5cefddaae40ed94ea713f7c8",
"derived_from" : "5cefddaae40ed94ea713f7c7",
"epsg" : "32723",
"active" : True,
"name" : "DPA-Time",
"range_time_depth" : [ 4648.91338, 5641.545770000001 ],
"type" : "Polygon",
"iline": 6998
},
"id_2" : {
"_id" : "5cefddaae40ed94ea713f7c9",
"derived_from" : "5cefddaae40ed94ea713f7c7",
"epsg" : "32723",
"active" : True,
"name" : "DPA-Time",
"range_time_depth" : [ 4648.91338, 5641.545770000001 ],
"type" : "Polygon",
"iline": 7002
}
}
query_ = {
'_id': { '$in': ['5cefddaae40ed94ea713f7c8','5cefddaae40ed94ea713f7c9'] },
'iline': {'$gte': 6998, '$lte': 7000}
}
def check_correspondence(obj, query):
is_in = obj['_id'] in query['_id']['$in']
is_iline = query['iline']['$lte'] >= obj['iline'] >= query['iline']['$gte']
return is_in and is_iline
for obj_name, obj_ in objs.items():
print(obj_name, check_correspondence(obj_, query_))
# id_1 True
# id_2 False - because 7002 > 7000
我有一本字典,它们的形式是:
"id_1" : {
"_id" : ObjectId("5cefddaae40ed94ea713f7c8"),
"derived_from" : ObjectId("5cefddaae40ed94ea713f7c7"),
"epsg" : "32723",
"active" : True,
"name" : "DPA-Time",
"range_time_depth" : [ 4648.91338, 5641.545770000001 ],
"type" : "Polygon",
"iline": 6998
}
我有一本像数据库查询一样工作的字典:
{
'_id': { '$in': ['5cefddaae40ed94ea713f7c8','5cefddaae40ed94ea713f7c8'] },
'iline': {'$gte': 6998, '$lte': 7000}
}
所以,我想检索词典字典中所有 _id
等于 '5cefddaae40ed94ea713f7c8'
或 '5cefddaae40ed94ea713f7c8'
和 'iline'
在 6998 和 7000 之间的词典。
我知道交集方法有助于匹配字典值,但是当我想将值与值的 range 匹配时,我该怎么做?
我做了什么:
def check_correspondence(obj, query):
for _, value in obj.items():
if value.keys() & query.keys():
return True
else: return False
我循环遍历提到的字典并应用 & 运算符。但是,此运算符似乎不适用于范围比较,只能用于严格相等。
当我尝试时,我收到 TypeError: unhashable type: 'list'
您不需要检查所有密钥,因为您只想检查 ID 和 iline
。
注意,&
和 and
在 python 中是不一样的。 &
是 bit-wise operator.
objs = {
"id_1" : {
"_id" : "5cefddaae40ed94ea713f7c8",
"derived_from" : "5cefddaae40ed94ea713f7c7",
"epsg" : "32723",
"active" : True,
"name" : "DPA-Time",
"range_time_depth" : [ 4648.91338, 5641.545770000001 ],
"type" : "Polygon",
"iline": 6998
},
"id_2" : {
"_id" : "5cefddaae40ed94ea713f7c9",
"derived_from" : "5cefddaae40ed94ea713f7c7",
"epsg" : "32723",
"active" : True,
"name" : "DPA-Time",
"range_time_depth" : [ 4648.91338, 5641.545770000001 ],
"type" : "Polygon",
"iline": 7002
}
}
query_ = {
'_id': { '$in': ['5cefddaae40ed94ea713f7c8','5cefddaae40ed94ea713f7c9'] },
'iline': {'$gte': 6998, '$lte': 7000}
}
def check_correspondence(obj, query):
is_in = obj['_id'] in query['_id']['$in']
is_iline = query['iline']['$lte'] >= obj['iline'] >= query['iline']['$gte']
return is_in and is_iline
for obj_name, obj_ in objs.items():
print(obj_name, check_correspondence(obj_, query_))
# id_1 True
# id_2 False - because 7002 > 7000