You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python列表数据清洗:如何删除以[t]开头的完整DVD标题元素

Python列表清洗实现方法

你可以直接通过列表推导式对原列表做过滤,仅保留非[t]开头的元素即可,操作如下:

  1. 首先定义你的原始列表:
original_list = ['[t] the little mermaid.','disney fairytale','price of this $5.99', '[t] star wars.','disney fantasy','price $6']
  1. 执行过滤逻辑,判断每个元素是否以[t]开头,过滤掉符合条件的元素:
cleaned_list = [item for item in original_list if not item.startswith('[t]')]

运行后cleaned_list就是你需要的结果:
['disney fairytale', 'price of this $5.99', 'disney fantasy', 'price $6']

补充说明

你之前操作只删除了[t]字符,大概率是对单个元素做了字符串替换操作,没有触发整个元素的移除逻辑。如果你需要原地修改原列表不想生成新列表,可以倒序遍历后调用pop方法删除对应元素,避免正序遍历删元素时的索引错乱问题:

# 倒序遍历原地删除
for i in range(len(original_list)-1, -1, -1):
    if original_list[i].startswith('[t]'):
        original_list.pop(i)

内容的提问来源于stack exchange,提问作者chip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 00:39:02