【问题标题】:How to remove a 2-tuple of int/float from a list in a loop?如何从循环中的列表中删除 2 元组 int/float?
【发布时间】:2020-05-18 04:34:56
【问题描述】:

如果我有一个清单:

x = [(1,3.),(2,4.),(5,6), (6,6.)]

我如何能够删除具有 6 的那些,因为它是第二个(或第一个,如果在索引方面)术语?它看起来像:

x = [(1,3.),(2,4.)]

我尝试过的代码总是给我错误:

TypeError: 'float' object is not subscriptable

TypeError: 'int' object is not subscriptable

【问题讨论】:

    标签: python list int tuples typeerror


    【解决方案1】:

    您可以使用列表推导:

    x = [item for item in x if item[1] != 6]
    

    【讨论】:

      猜你喜欢
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 2014-09-08
      • 2018-02-19
      • 2014-08-26
      • 1970-01-01
      • 1970-01-01
      • 2017-02-08
      相关资源
      最近更新 更多