小编Bla*_*ght的帖子

当键是元组时搜索字典键

sample = {('red', 'blue', 'purple') : 'color', 'redo' : 'again', 'bred' : 'idk', 'greeting' : ('hi', 'hello')}

def search(c):
    if c in sample.keys():
        return sample[c]

print(search('red'))
Run Code Online (Sandbox Code Playgroud)

这返回None. 我知道我可以将它们分开并制作具有相同值的多个键,但如果可以的话,我真的很想避免这样做。我可以吗?

而且我还希望能够搜索值(也可能是元组)并获得相应的键。

python dictionary python-3.x

5
推荐指数
2
解决办法
1550
查看次数

标签 统计

dictionary ×1

python ×1

python-3.x ×1