Python 列表过滤删除太多

时间:2021-02-01 21:29:00

标签: python-3.x list filtering

我将 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']

我可以更改什么以只删除带有 // 的字符串?

1 个答案:

答案 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']