有没有办法只从 python 列表中输出数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1277914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:50:35  来源:igfitidea点击:

Is there a way to output the numbers only from a python list?

python

提问by dassouki

Simple Question:

简单的问题:

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

I want to create a list_2such that it only contains the numbers:

我想创建一个list_2只包含数字的这样的:

list_2 = [ 1, 123131.131, 0.000001, 34.12451235265 ]

Is there simplistic way of doing this, or do I have to resort to checking the variable type of each list item and only output the numerical ones?

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

回答by dalloliogm

This should be the most efficent and shortest:

这应该是最有效和最短的:

import operator
filter(operator.isNumberType, list_1)

Edit: this in python 3000:

编辑:这在python 3000中:

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

回答by Ants Aasma

List comprehensions.

列出理解。

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

回答by SilentGhost

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

回答by J0ANMM

All solutions proposed work only if the numbers inside the list are already converted to the appropriate type (int, float).

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

I found myself with a list coming from the fetchallfunction of sqlite3. All elements there were formated as str, even if some of those elements were actually integers.

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

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

The equivalent from the question would be having something like:

问题中的等价物将具有以下内容:

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

For such a case, in order to get a list with the integers only, this is the alternative I found:

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

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

list_of_numberswill contain only all integers from the initial list.

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

回答by Pontios

I think the easiest way is:

我认为最简单的方法是:

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

Hope it helps!

希望能帮助到你!

回答by Jeff Ober

filter(lambda n: isinstance(n, int), [1,2,"three"])

回答by sykora

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

回答by ghostdog74

>>> [ i for i in list_1 if not str(i).replace(" ","").isalpha() ]
[1, 123131.13099999999, 9.9999999999999995e-07, 34.124512352650001]

回答by wearetherock

for short of SilentGhost way

简称 SilentGhost 方式

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

to

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