【问题标题】:Is there a way to output the numbers only from a python list?有没有办法只从 python 列表中输出数字?
【发布时间】:2021-08-02 21:26:01
【问题描述】:

简单的问题:

list_1 = [ 'asdada', 1, 123131.131, 'blaa adaraerada', 0.000001, 34.12451235265, 'stackoverflow is awesome' ]

我想创建一个 list_2 使其只包含数字:

list_2 = [ 1, 123131.131, 0.000001, 34.12451235265 ]

有没有简单的方法可以做到这一点,还是我必须求助于检查每个列表项的变量类型并且只输出数字的?

【问题讨论】:

  • 为什么这个问题被否决了??
  • 该死,这是我希望我可以选择两个答案的时候
  • 为什么现在投票率这么高?这是这里许多问题的重复!

标签: python


【解决方案1】:

列表推导。

list_2 = [num for num in list_1 if isinstance(num, (int,float))]

【讨论】:

  • 可能值得将类型 long 添加到您的元组中。
  • 为了改善这一点,您可以使用 operator.isNumberType 来确定元素是否为数字(请参阅我的其他答案,使用过滤器而不是列表理解来做到这一点)。
【解决方案2】:

这应该是 Python 2 中最高效、最短的:

import operator
filter(operator.isNumberType, list_1)

这在 Python 3 中:

import numbers
[x for x in list_1 if isinstance(x, numbers.Number)]

【讨论】:

  • 我不使用 py3k,但它只是 isinstance(x, numbers.Number) 而不是 operator.isNumberType)
  • ok..我对python 3k了解不多,所以又改了例子。
【解决方案3】:
list_2 = [i for i in list_1 if isinstance(i, (int, float))]

【讨论】:

    【解决方案4】:

    我认为最简单的方法是:

    [s for s in myList if s.isdigit()]
    

    希望对你有帮助!

    【讨论】:

      【解决方案5】:

      仅当列表中的数字已转换为适当的类型(intfloat)时,所有建议的解决方案才有效。

      我发现自己的列表来自sqlite3fetchall 函数。那里的所有元素都被格式化为str,即使其中一些元素实际上是整数。

      cur.execute('SELECT column1 FROM table1 WHERE column2 = ?', (some_condition, ))
      list_of_tuples = cur.fetchall()
      

      问题的等价物是这样的:

      list_1 = [ 'asdada', '1', '123131', 'blaa adaraerada', '0', '34', 'stackoverflow is awesome' ]
      

      对于这种情况,为了获得仅包含整数的列表,这是我找到的替代方法:

      list_of_numbers = []
      for tup in list_of_tuples:
          try:
              list_of_numbers.append(int(tup[0]))
          except ValueError:
              pass
      

      list_of_numbers 将仅包含初始列表中的所有整数。

      【讨论】:

        【解决方案6】:
        filter(lambda n: isinstance(n, int), [1,2,"three"])
        

        【讨论】:

        • 测试各种类型:filter(lambda n: isinstance(n, int) or isinstance(n, float), list_of_values)
        • 我指的是一般的数字,而不仅仅是整数
        【解决方案7】:
        list_2 = [i for i in list_1 if isinstance(i, (int, float))]
        

        【讨论】:

        • isinstance 只接受 2 个参数。
        • 大概意思是:list_2 = [i for i in list_1 if isinstance(i, (int, float))]
        【解决方案8】:
        >>> [ i for i in list_1 if not str(i).replace(" ","").isalpha() ]
        [1, 123131.13099999999, 9.9999999999999995e-07, 34.124512352650001]
        

        【讨论】:

          【解决方案9】:

          SilentGhost 方式的简称

          list_2 = [i for i in list_1 if isinstance(i, (int, float))] 
          

          list_2 = [i for i in list_1 if not isinstance(i, str)]
          

          【讨论】:

            【解决方案10】:
            list_1 = [ 'asdada', 1, 123131.131, 'blaa adaraerada', 0.000001, 34.12451235265, 'stackoverflow is awesome' ]
            

            第一个例子:

            list_2 = [x for x in list_1 if type(x) == int or type(x) == float ]
            print(list_2)
            

            第二个例子:

            list_2 = [x for x in list_1 if isinstance(x, (int, float)) ]
            print(list_2)
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-02-29
              • 2020-04-16
              • 1970-01-01
              • 2021-03-13
              • 2021-07-16
              • 1970-01-01
              相关资源
              最近更新 更多