首页 > 解决方案 > python中列表的数据清理

问题描述

我已经阅读了 dvd 标题描述和价格作为列表中的句子,DVD 的标题以 [t] 开头,例如: ['[t] the little mermaid.','disney fairytale','price of this $5.99', '[t] star wars.','disney fantasy','price $6']

如何从列表中删除整个标题.. 所以以 [t] 开头的项目因此列表将只有描述和价格。

我尝试使用 list.remove 但这仅删除了 [t] 而不是 dvd 标题

标签: pythonlist

解决方案


你可以使用startswith

lst = ['[t] the little mermaid.','disney fairytale','price of this $5.99', '[t] star wars.','disney fantasy','price $6']
print([i for i in lst if not i.startswith('[t] ')])

输出:

['disney fairytale', 'price of this $5.99', 'disney fantasy', 'price $6']

推荐阅读