假设我有一个python列表&;字典结构如下:
[ {'href': 'https://www.simplyrecipes.com/recipes/cuisine/portuguese/'},
{'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/'}]
我正在努力找到最有效的方法来
(i)仅循环遍历='href'
的键,以及仅循环值包含'https://www.simplyrecipes.com/recipes/'
的'href'
键,并标识包含'recipes/cuisine'
、'recipes/season'
和'recipes/ingredient'
的值('http...'
)
(ii)将每个完整url值保存到单独的列表中(取决于它们满足的'recipe/...'
条件),并命名为适当的
预期结果:
cuisine = ['https://www.simplyrecipes.com/recipes/cuisine/portuguese/']
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/']
跳过任何不符合上述条件的键和/或值。
任何指点都将不胜感激
所以大致上
假设URL与所附问题中的格式相同。更好的方法是创建一个不同食谱的目录
这里有一个简单的例子,希望对您有所帮助
输出
相关问题 更多 >
编程相关推荐