【问题标题】:Python remove() not removing all items in the list [duplicate]Python remove()没有删除列表中的所有项目[重复]
【发布时间】:2022-10-04 20:41:02
【问题描述】:

所以我想从数字中删除所有 3s

numbers = [3, 3, 3, 3, 3, 1, 2, 2, 3, 3, 3, 3, 3, 3]
for i in numbers:
    if i == 3:
        numbers.remove(3)
print(numbers)

感觉应该这样做,但它会打印

[1, 2, 2, 3, 3, 3, 3, 3]

结果..似乎连续三分是问题,但我不知道为什么..

【问题讨论】:

    标签: python


    【解决方案1】:
    result = [i for i in numbers if i != 3]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      • 2020-11-10
      • 1970-01-01
      • 2014-02-23
      • 2011-09-29
      • 2013-08-03
      相关资源
      最近更新 更多