my_dict = {a={"item1": "AAABC", "item2": "AAL3example", "item3": "XYZ"}}提取字典中元组中任何字符存在”AAL3“的项
时间: 2024-06-16 14:01:54 浏览: 16
要在Python字典`my_dict`中找到包含"AAAL3"子串的项,我们可以遍历这个嵌套字典结构。由于`a`键下的值是一个字典,我们需要先访问这个字典,然后检查其子项的值是否包含"AAAL3"。这里是一个示例代码片段来实现这个任务:
```python
my_dict = {a: {"item1": "AAABC", "item2": "AAL3example", "item3": "XYZ"}}
search_string = "AAAL3"
result = []
def search_nested_dict(nested_dict, target):
for key, value in nested_dict.items():
if isinstance(value, dict):
if search_string in str(value).lower(): # 将字典转换为字符串并忽略大小写搜索
result.append((key, value))
elif isinstance(value, str) and search_string in value.lower():
result.append((key, value))
# 遍历字典
search_nested_dict(my_dict[a], search_string)
# 输出结果
if result:
print(f"找到以下包含'{search_string}'的项:")
for item_key, item_value in result:
print(f"{item_key}: {item_value}")
else:
print("没有找到匹配的项。")
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)