我将 url 的 python 列表作为字符串。我正在尝试删除所有包含两个正斜杠 (//) 的字符串。这是我尝试这样做的方式:
filtered_list = [x for x in original_list if 'https://www.ourlads.com/ncaa-football-depth-charts/player//' not in x]
但是,当我运行它时,它会删除所有带有 // 的字符串以及甚至不包含 // 的其他字符串。
这是原始列表的示例:
original_list = ['https://www.ourlads.com/ncaa-football-depth-charts/player/devonta-smith/123433',
'https://www.ourlads.com/ncaa-football-depth-charts/player//0',
'https://www.ourlads.com/ncaa-football-depth-charts/player//116922',
'https://www.ourlads.com/ncaa-football-depth-charts/player/alex-leatherwood/123411']
我可以更改什么以只删除带有 // 的字符串?
答案 0 :(得分:1)
您的代码似乎正在运行。 但另一种方法是通过正则表达式。
import re
original_list = ['https://www.ourlads.com/ncaa-football-depth-charts/player/devonta-smith/123433',
'https://www.ourlads.com/ncaa-football-depth-charts/player//0',
'https://www.ourlads.com/ncaa-football-depth-charts/player//116922',
'https://www.ourlads.com/ncaa-football-depth-charts/player/alex-leatherwood/123411']
filtered_list = [x for x in original_list if not re.match(r"^https://.*//", x)]
filtered_list
过滤器列表:
['https://www.ourlads.com/ncaa-football-depth-charts/player/devonta-smith/123433',
'https://www.ourlads.com/ncaa-football-depth-charts/player/alex-leatherwood/123411']