Python 在两个字典中搜索项并基于匹配返回结果
如何检查位置(例如(1,5))是否存在于两个字典“co_1和co_2”的值中 到目前为止,我已经:Python 在两个字典中搜索项并基于匹配返回结果,python,performance,dictionary,search,Python,Performance,Dictionary,Search,如何检查位置(例如(1,5))是否存在于两个字典“co_1和co_2”的值中 到目前为止,我已经: co_1 = {'a1': [(1, 1)], 'b1': [(0, 4), (0, 0), (4, 0)]} co_2 = {'a2': [(2, 2)], 'b2': [(1, 5), (1, 2), (5, 1)]} position = (x, y) 有没有办法清理这个问题,这样我就可以在两个字典中一起搜索位置,然后有一个if-else语句:if-position-present-in
co_1 = {'a1': [(1, 1)], 'b1': [(0, 4), (0, 0), (4, 0)]}
co_2 = {'a2': [(2, 2)], 'b2': [(1, 5), (1, 2), (5, 1)]}
position = (x, y)
有没有办法清理这个问题,这样我就可以在两个字典中一起搜索位置,然后有一个if-else语句:if-position-present-in-values(co_1或co_2)return(statement)else-note
例如:
for key, value in co_1.items():
if position in value:
return (statement1)
for key, value in co_1.items():
if position in value:
return(statement2)
#if position not in either value --> return None
这里的键似乎不相关——您可以从两个字典中构建一组元组
for key, value in co_1.items() and co_2.items():
if position in value in co_1.items():
return statement1
elif position in value in co_2.items():
return statement2
else:
return None
#example if position = (2, 2) --> return statement2
#exmaple if position = (3, 1) --> return None
现在,成员资格测试就像if elif
语句一样简单:
co_1_set = {y for x in co_1 for y in co_1[x] }
co_2_set = {y for x in co_2 for y in co_2[x] }
您将希望尽可能少地执行set
构造—理想情况下仅当字典内容发生更改时
如果两个词典都包含
position
,则此代码仅返回statement1
。如果您想要不同的内容,则需要进行适当的更改。您可以使用每个dict的展平值,如下所示:
def foo(position):
if position in co_1_set:
return statement1
elif position in co_2_set:
return statement2
尝试使用生成器:
co_1 = {'a1': [(1, 1)], 'b1': [(0, 4), (0, 0), (4, 0)]}
co_2 = {'a2': [(2, 2)], 'b2': [(1, 5), (1, 2), (5, 1)]}
def check_position(pos):
if pos in [item for sublist in co_1.values() for item in sublist]:
return 'statement1'
elif pos in [item for sublist in co_2.values() for item in sublist]:
return 'statement2'
else:
return None
pos1 = (2, 2)
pos2 = (0, 4)
print(check_position(pos1)) # Output: statement2
print(check_position(pos2)) # Output: statement1
在这种情况下,您将收到所有找到的语句
如果您只需要第一次出现:
def search(item, co_1, co_2):
co_1_values = co_1.values()
co_2_values = co_2.values()
for value in list(co_1_values) + list(co_2_values):
yield 'statement1' if item in value in co_1_values else ''
yield 'statement2' if item in value in co_2_values else ''
另一种方法:sum(v中的pos代表参数值中的v())
是一种成员资格测试,返回0
,当未找到pos
时,返回==False
下面用作列表理解中的条件,返回输入的*args
for item in search((1, 1), co_1, co_2):
if item.strip():
print(item)
break
您可以使用2个布尔变量
co_1 = {'a1': [(1, 1)], 'b1': [(0, 4), (0, 0), (4, 0)]}
co_2 = {'a2': [(2, 2)], 'b2': [(1, 5), (1, 2), (5, 1)]}
pos = (1, 5)
def q(pos, *args):
return [i for i, arg in enumerate(args)
if sum(pos in v for v in arg.values())]
q(pos, co_1, co_2)
Out[184]: [1]
q((0, 0), co_1, co_2)
Out[185]: [0]
q((0, 10), co_1, co_2)
Out[186]: []
您的新代码以何种方式“更干净”?两个dict的字典键是否始终相同?如果两个键都包含
position
你会返回什么?下一步((s代表s,d在zip中((statement1,statement2),(co_1,co_2))代表v在d中。value()如果position在v中,则为None)
:p在提取完所有值后,为什么搜索组合的项目列表,只忽略键?当你找到一个匹配项时,搜索它来自哪个集合?这比原来的代码好多少?谢谢!这被认为比在我的原始查询中使用两个“for”语句更好吗?我对python有点陌生,还没有完全理解列表。有没有可能您还可以包括一个扩展版本,这样我就可以看到它是如何完成的。@turtlepower它更好,因为基本上,您可以缓存结果以便快速访问。这就像做:s=set();对于co_1中的x:对于co_1中的y[x]:S.add(y)
co_1 = {'a1': [(1, 1)], 'b1': [(0, 4), (0, 0), (4, 0)]}
co_2 = {'a2': [(1, 2)], 'b2': [(1, 5), (1, 2), (5, 1)]}
position = (1,1)
tco1=False
tco2=False
for key, value in co_1.items():
if position in value:
tco1=True
for key, value in co_2.items():
if position in value:
tco2=True
if tco1==True:
print("Statement 1") #Or return Statement 1
elif tco2==True:
print("Statement 2") #Or return Statement 2
else :
print("None") #Or return None