如何删除列表中的特定值?

时间:2021-05-10 02:57:43

标签: python

如何删除列表中的特定值?

列表的值存在。

review_list = ["14hour","16hour","20hour","24hour","8hour","4hour","2hour","hello","jae12"]

如果您查看列表值,

只有数值在 int(1-24) 小时之前发生变化。

如何删除 int (1-24)hour 并输出剩余的值?

输出

review_list = "hello","jae12"]

2 个答案:

答案 0 :(得分:1)

根据您的预期输出,您想要删除值中包含“小时”的元素

review_list = ["14hour","16hour","20hour","24hour","8hour","4hour","2hour","hello","jae12"]
review_filtered = [x for x in review_list if "hour" not in x]

输出

["hello", "jae12"]

答案 1 :(得分:0)

review_list = list(filter(lambda item: not item.endswith("hour"), review_list))

import re
review_list = list(filter(lambda item: not re.match('^[0-9]+hour$',item), review_list))