假设您已保存 中的所有值,请dict说:dict = { a: [(21, ['one', 'two', 'three'])] b: [(21, ['four', 'five'])]}然后,要访问给定值的键,您需要反转它们的关系(这将加快查找速度以换取更多内存):lookup = {}for key in dict.keys(): for value in dict[key][0][1]: #this is the list inside the tuple inside the list lookup[value] = key所以,当你在寻找所需值的键时,你可以去:print('out:', lookup['three'])这将输出:out: a
您可以通过迭代字典中的每个项目来做到这一点,但是在大型数据集中它可能效率低下。def get_key(data, query): for key, value in data.items(): if query in value[0][1]: return key return 'Not Found'get_key(dictonary, word)然后,即使您的查找未能找到匹配项,您也可以调用您的函数并返回结果。# Note i changed the name of the dictionary to dicton, as dict shouldn't be used as a variable nameprint(get_key(dicton, 'three'))print(get_key(dicton, 'seven'))print(get_key(dicton, 'four'))#a#Not Found#b