遍历嵌套字典以检查特定值是否全部为数字字母
Iterating through a nested dictionary to check if a particular value is all numerical letters
以下是我拥有的一个非常大的嵌套字典的子集:
{
'1': {'Name': 'Katherine Watson',
'Age': '1',
'Height': '150'},
'2': {'Name': 'Emilia Li',
'Age': '56',
'Height': '175'},
'3': {'Name': 'Dorothy Johnson',
'Age': '29',
'Height': '162'},
'4': {'Name': 'Alexandar Knight',
'Age': '14',
'Height': '164r'}
}
我无法弄清楚如何编写一个函数来遍历特定键 ('Height'),然后 return 如果它是所有数字或 None 否则。
例如。 ID为'1'的字典的高度应该是return'150'。但是ID为'4'的字典的高度应该是return None.
这是我写的代码,但它只是 returns '150' 而不是遍历所有 ID 和 returning '150' '175' '162' 'None'.
data = {
'1': {'Name': 'Katherine Watson',
'Age': '1',
'Height': '150'},
'2': {'Name': 'Emilia Li',
'Age': '56',
'Height': '175'},
'3': {'Name': 'Dorothy Johnson',
'Age': '29',
'Height': '162'},
'4': {'Name': 'Alexandar Knight',
'Age': '14',
'Height': '164r'}
}
def person_height(height):
for some_id, info in data.items():
if info['Height'].isnumeric():
return info['Height']
else:
return None
使用isdigit
data = {
'1': {'Name': 'Katherine Watson',
'Age': '1',
'Height': '150'},
'2': {'Name': 'Emilia Li',
'Age': '56',
'Height': '175'},
'3': {'Name': 'Dorothy Johnson',
'Age': '29',
'Height': '162'},
'4': {'Name': 'Alexandar Knight',
'Age': '14',
'Height': '164r'}
}
def person_height(height):
if height.isdigit():
return height
for some_id, info in data.items():
print("\nID:", some_id)
print("Height:", person_height(info['Height']))
输出:
ID: 1
Height: 150
ID: 2
Height: 175
ID: 3
Height: 162
ID: 4
Height: None
为了将结果存储在“干净”的字典中,您需要的嵌套循环与您拥有的嵌套字典一样多。所以:
def check_height(your_dict):
new_clean_dict = {}
for a, b in your_dict.items():
for e, f in b.items():
if e == 'Height' and f.isdigit():
new_clean_dict[a] = {'Height': f}
else:
new_clean_dict[a] = {'Height': None}
return new_clean_dict
这将生成一个具有相同根键且每个键都有一个值的新字典,这是一个只有键 Height
的嵌套字典。
为了获得结果:
new_clean_dict = check_height(your_dict)
您的代码实际上没问题,但是 return
会立即中断循环,并且 return 只是第一个结果,所以只需将您的 return
转换为 print()
即可.
另一种方法是先将结果保存到列表中,然后再读取它们:
data = {
'1': {'Name': 'Katherine Watson',
'Age': '1',
'Height': '150'},
'2': {'Name': 'Emilia Li',
'Age': '56',
'Height': '175'},
'3': {'Name': 'Dorothy Johnson',
'Age': '29',
'Height': '162'},
'4': {'Name': 'Alexandar Knight',
'Age': '14',
'Height': '164r'}
}
def person_height(data):
height_list = []
for some_id, info in data.items():
if info['Height'].isnumeric():
height_list.append(info['Height'])
else:
height_list.append(None)
return height_list
for height in person_height(data):
print(height)
输出:
150
175
162
None
您也可以使用 list comprehension。
def get_heights(data):
return [int(person['Height'])
if person['Height'].isdigit()
else None
for person in data.values()]
print(get_heights(data))
运行 它与您的示例数据输出:
[150, 175, 162, None]
由于您不使用 ID,因此可以使用 .values()
而不是 .items()
。在您的代码中,您将参数命名为 height
但随后在函数体中引用了 data
。这意味着您提供什么作为参数并不重要;该代码之所以有效,是因为它引用了全局定义的变量,而该变量恰好具有相同的名称。
我还将高度转换为整数,即使您没有特别要求。
以下是我拥有的一个非常大的嵌套字典的子集:
{
'1': {'Name': 'Katherine Watson',
'Age': '1',
'Height': '150'},
'2': {'Name': 'Emilia Li',
'Age': '56',
'Height': '175'},
'3': {'Name': 'Dorothy Johnson',
'Age': '29',
'Height': '162'},
'4': {'Name': 'Alexandar Knight',
'Age': '14',
'Height': '164r'}
}
我无法弄清楚如何编写一个函数来遍历特定键 ('Height'),然后 return 如果它是所有数字或 None 否则。 例如。 ID为'1'的字典的高度应该是return'150'。但是ID为'4'的字典的高度应该是return None.
这是我写的代码,但它只是 returns '150' 而不是遍历所有 ID 和 returning '150' '175' '162' 'None'.
data = {
'1': {'Name': 'Katherine Watson',
'Age': '1',
'Height': '150'},
'2': {'Name': 'Emilia Li',
'Age': '56',
'Height': '175'},
'3': {'Name': 'Dorothy Johnson',
'Age': '29',
'Height': '162'},
'4': {'Name': 'Alexandar Knight',
'Age': '14',
'Height': '164r'}
}
def person_height(height):
for some_id, info in data.items():
if info['Height'].isnumeric():
return info['Height']
else:
return None
使用isdigit
data = {
'1': {'Name': 'Katherine Watson',
'Age': '1',
'Height': '150'},
'2': {'Name': 'Emilia Li',
'Age': '56',
'Height': '175'},
'3': {'Name': 'Dorothy Johnson',
'Age': '29',
'Height': '162'},
'4': {'Name': 'Alexandar Knight',
'Age': '14',
'Height': '164r'}
}
def person_height(height):
if height.isdigit():
return height
for some_id, info in data.items():
print("\nID:", some_id)
print("Height:", person_height(info['Height']))
输出:
ID: 1
Height: 150
ID: 2
Height: 175
ID: 3
Height: 162
ID: 4
Height: None
为了将结果存储在“干净”的字典中,您需要的嵌套循环与您拥有的嵌套字典一样多。所以:
def check_height(your_dict):
new_clean_dict = {}
for a, b in your_dict.items():
for e, f in b.items():
if e == 'Height' and f.isdigit():
new_clean_dict[a] = {'Height': f}
else:
new_clean_dict[a] = {'Height': None}
return new_clean_dict
这将生成一个具有相同根键且每个键都有一个值的新字典,这是一个只有键 Height
的嵌套字典。
为了获得结果:
new_clean_dict = check_height(your_dict)
您的代码实际上没问题,但是 return
会立即中断循环,并且 return 只是第一个结果,所以只需将您的 return
转换为 print()
即可.
另一种方法是先将结果保存到列表中,然后再读取它们:
data = {
'1': {'Name': 'Katherine Watson',
'Age': '1',
'Height': '150'},
'2': {'Name': 'Emilia Li',
'Age': '56',
'Height': '175'},
'3': {'Name': 'Dorothy Johnson',
'Age': '29',
'Height': '162'},
'4': {'Name': 'Alexandar Knight',
'Age': '14',
'Height': '164r'}
}
def person_height(data):
height_list = []
for some_id, info in data.items():
if info['Height'].isnumeric():
height_list.append(info['Height'])
else:
height_list.append(None)
return height_list
for height in person_height(data):
print(height)
输出:
150
175
162
None
您也可以使用 list comprehension。
def get_heights(data):
return [int(person['Height'])
if person['Height'].isdigit()
else None
for person in data.values()]
print(get_heights(data))
运行 它与您的示例数据输出:
[150, 175, 162, None]
由于您不使用 ID,因此可以使用 .values()
而不是 .items()
。在您的代码中,您将参数命名为 height
但随后在函数体中引用了 data
。这意味着您提供什么作为参数并不重要;该代码之所以有效,是因为它引用了全局定义的变量,而该变量恰好具有相同的名称。
我还将高度转换为整数,即使您没有特别要求。