【发布时间】:2019-03-12 03:16:48
【问题描述】:
我有一个字典,我想搜索与我需要的特定值相对应的键(在本例中为 S009 和 S007)
我写了以下代码,但我没有从中得到任何东西
这是我的代码:
def find():
L = [{"V": "S001"},
{"V": "S002"},
{"V": "S001"},
{"V": "S001"},
{"V": "S001"},
{"V1": "S002"},
{"V111": "S005"},
{"V2": "S005"},
{"V": "S009"},
{"V3": "S007"}]
L1 = []
for y in range (len(L)) :
for j in L[y].values():
L1.append(j)
L2=[]
for z in L1:
if z not in L2:
L2.append(z)
count =0
l3=[]
s = set(L1)
for z in L2:
for y in L1:
if z in L2:
count =count +1
if count == 2:
l3.append(z)
for s in l3:
print(s)
def main():
find()
main()
我的代码解释:首先,我将列表中的所有值命名为L1。然后我得到所有值而不被复制到L2。然后,我想搜索L2 的元素是否存在于L1 中。在这个循环之后,如果 count 变成只有一个,那么这就是我正在寻找的值,我将它附加到一个名为 l3 的空列表中
【问题讨论】:
-
请修正你的缩进。
-
这是我做过的最可怕的编辑。请在发布之前格式化您的问题和代码
标签: python function dictionary search