字典内查找值等于
时间: 2023-07-18 14:10:16 浏览: 58
搜索一维数组中相同的值
如果您想在 Python 字典中查找某个键对应的值,可以使用如下的代码:
```python
my_dict = {"apple": 1, "banana": 2, "orange": 3}
key_to_find = "banana"
if key_to_find in my_dict:
value = my_dict[key_to_find]
print(f"The value of {key_to_find} is {value}.")
else:
print(f"{key_to_find} is not found in the dictionary.")
```
如果您想查找字典中的某个值,可以使用字典的 `values()` 方法遍历所有的值,例如:
```python
my_dict = {"apple": 1, "banana": 2, "orange": 3}
value_to_find = 2
if value_to_find in my_dict.values():
for key, value in my_dict.items():
if value == value_to_find:
print(f"The key of {value_to_find} is {key}.")
else:
print(f"{value_to_find} is not found in the dictionary.")
```
阅读全文