Python字典全量值检索:如何查找含None值的条目
Hey there! Let's break down your problem step by step to get you the solution you need.
First, I spotted a critical detail in your example dictionary: the values you're targeting are 'None' (string literals) instead of Python's actual None keyword. That’s likely why your current code isn’t working—you’re checking for the None object, but your data contains string versions of "None" instead. Let’s fix that, plus handle deep traversal for nested dictionaries too.
1. Basic Case: Flat Dictionary
If your dictionary isn’t nested, we can directly filter keys based on whether their values match your target (either the string 'None' or Python’s None):
# Your sample dictionary (note: keys should be strings in valid Python) sample_dict = { 'data01': 'full', 'data02': 'None', 'data03': 'high', 'data04': 'None' } # Option 1: Find keys with string 'None' values exclude_keys = [key for key, value in sample_dict.items() if value == 'None'] # Option 2: If your data uses actual Python None instead of strings # exclude_keys = [key for key, value in sample_dict.items() if value is None] print(exclude_keys) # Output: ['data02', 'data04']
2. Deep Traversal: Nested Dictionaries
If your JSON input has nested dictionaries (which is common in real-world data), we’ll need a recursive function to dig through every level and collect all matching keys—including their full path so you know exactly where they are:
def find_none_entries(data, parent_key=''): exclude_keys = [] for key, value in data.items(): # Build a full path for nested keys (e.g., "level1.level2.data05") current_key = f"{parent_key}.{key}" if parent_key else key if isinstance(value, dict): # Recursively check nested dictionaries and merge results exclude_keys.extend(find_none_entries(value, current_key)) else: # Check for both string 'None' and Python None (adjust based on your data) if value == 'None' or value is None: exclude_keys.append(current_key) return exclude_keys # Test with a nested dictionary nested_dict = { 'data01': 'full', 'data02': 'None', 'level1': { 'data03': None, 'data04': 'high', 'level2': { 'data05': 'None' } } } print(find_none_entries(nested_dict)) # Output: ['data02', 'level1.data03', 'level1.level2.data05']
Bonus: Fix JSON Parsing (If Needed)
If your inputfromjson is converting JSON null values to the string 'None' (instead of Python’s None), you can fix this during parsing with a custom hook:
import json def convert_none_strings(obj): if isinstance(obj, dict): # Convert any 'None' strings to actual None objects return {k: v if v != 'None' else None for k, v in obj.items()} return obj # Parse your JSON with the hook json_str = '{"data01": "full", "data02": "None", "data03": "high", "data04": "None"}' input_from_json = json.loads(json_str, object_hook=convert_none_strings) # Now you can check for actual None values exclude_keys = [key for key, value in input_from_json.items() if value is None] print(exclude_keys) # Output: ['data02', 'data04']
内容的提问来源于stack exchange,提问作者vonanka

