发展一个 for 循环以跳过不等于的字典键和值
Evolving a for loop to skip dict key and values that are not equal to
背景
我的结构如下:
trash = [ {'href': 'https://www.simplyrecipes.com/recipes/cuisine/portuguese/'},
{'href': 'https://www.simplyrecipes.com/recipes/cuisine/german/'},
{'href': 'https://www.simplyrecipes.com/recipes/season/seasonal_favorites_spring/'},
{'href': 'https://www.simplyrecipes.com/recipes/type/condiment/'},
{'href': 'https://www.simplyrecipes.com/recipes/ingredient/adobado/'}]
{'href': 'https://www.simplyrecipes.com/',
'title': 'Simply Recipes Food and Cooking Blog', 'rel': ['home']},]
如您所见,大多数键是 'href'
,大多数值包含 'https://www.simplyrecipes.com/recipes/'
。问题是那些不符合此命名约定的键和值...
代码:
此代码遍历结构并使用 re.findall
获取 'recipes/'
和 [=] 之间的字符串值17=] 为其对应的值创建一个新的键名。
for x in trash:
for y in x.values():
txt = ''
for i in re.findall("recipes/.*", y):
txt += i
title = txt.split('/')[1]
print({title: y})
输出:
假设我删除了不符合命名约定的 keys
和 values
'href'
并包含 'https://www.simplyrecipes.com/recipes/'
的字符串值,代码工作正常,如下所示:
{'cuisine': 'https://www.simplyrecipes.com/recipes/cuisine/portuguese/'}
{'cuisine': 'https://www.simplyrecipes.com/recipes/cuisine/german/'}
{'season': 'https://www.simplyrecipes.com/recipes/season/seasonal_favorites_spring/'}
{'type': 'https://www.simplyrecipes.com/recipes/type/condiment/'}
{'ingredient': 'https://www.simplyrecipes.com/recipes/ingredient/adobado/'}
问题:
代码的问题是,如果结构具有未确认的键和值,我会得到 TypeError: expected string or bytes-like object
代码中的命名约定。
问题:
我将如何改进此代码,以便它会跳过任何不存在的键t 命名为 'href'
,如果它们命名为 'href'
,如果它们的值不包含 'https://www.simplyrecipes.com/recipes/'
,将跳过?
for d in trash:
for key, value in d.items():
if key != "href" or "https://www.simplyrecipes.com/recipes/" not in value:
continue # Move onto next iteration
txt = ''
for i in re.findall("recipes/.*", value):
txt += i
title = txt.split('/')[1]
print({title: value})
您可以使用 dict.items()
遍历字典的键和值。然后你可以使用 if
语句来检查你的条件,如果不满足则 continue
。
背景
我的结构如下:
trash = [ {'href': 'https://www.simplyrecipes.com/recipes/cuisine/portuguese/'},
{'href': 'https://www.simplyrecipes.com/recipes/cuisine/german/'},
{'href': 'https://www.simplyrecipes.com/recipes/season/seasonal_favorites_spring/'},
{'href': 'https://www.simplyrecipes.com/recipes/type/condiment/'},
{'href': 'https://www.simplyrecipes.com/recipes/ingredient/adobado/'}]
{'href': 'https://www.simplyrecipes.com/',
'title': 'Simply Recipes Food and Cooking Blog', 'rel': ['home']},]
如您所见,大多数键是 'href'
,大多数值包含 'https://www.simplyrecipes.com/recipes/'
。问题是那些不符合此命名约定的键和值...
代码:
此代码遍历结构并使用 re.findall
获取 'recipes/'
和 [=] 之间的字符串值17=] 为其对应的值创建一个新的键名。
for x in trash:
for y in x.values():
txt = ''
for i in re.findall("recipes/.*", y):
txt += i
title = txt.split('/')[1]
print({title: y})
输出:
假设我删除了不符合命名约定的 keys
和 values
'href'
并包含 'https://www.simplyrecipes.com/recipes/'
的字符串值,代码工作正常,如下所示:
{'cuisine': 'https://www.simplyrecipes.com/recipes/cuisine/portuguese/'}
{'cuisine': 'https://www.simplyrecipes.com/recipes/cuisine/german/'}
{'season': 'https://www.simplyrecipes.com/recipes/season/seasonal_favorites_spring/'}
{'type': 'https://www.simplyrecipes.com/recipes/type/condiment/'}
{'ingredient': 'https://www.simplyrecipes.com/recipes/ingredient/adobado/'}
问题:
代码的问题是,如果结构具有未确认的键和值,我会得到 TypeError: expected string or bytes-like object
代码中的命名约定。
问题:
我将如何改进此代码,以便它会跳过任何不存在的键t 命名为 'href'
,如果它们命名为 'href'
,如果它们的值不包含 'https://www.simplyrecipes.com/recipes/'
,将跳过?
for d in trash:
for key, value in d.items():
if key != "href" or "https://www.simplyrecipes.com/recipes/" not in value:
continue # Move onto next iteration
txt = ''
for i in re.findall("recipes/.*", value):
txt += i
title = txt.split('/')[1]
print({title: value})
您可以使用 dict.items()
遍历字典的键和值。然后你可以使用 if
语句来检查你的条件,如果不满足则 continue
。