如何测试列表中的每个项目是否都是“int”类型?

发布于 2024-11-07 08:36:04 字数 239 浏览 1 评论 0原文

假设我有一个数字列表。我该如何检查列表中的每个项目都是 int?
我四处搜寻,但没有找到任何与此相关的信息。

for i in myList:
  result=isinstance(i, int)
  if result == False:
    break

可以,但在我看来看起来非常丑陋且不符合Python风格。
有没有更好的(并且更Pythonic)的方法来做到这一点?

Say I have a list of numbers. How would I do to check that every item in the list is an int?
I have searched around, but haven't been able to find anything on this.

for i in myList:
  result=isinstance(i, int)
  if result == False:
    break

would work, but looks very ugly and unpythonic in my opinion.
Is there any better(and more pythonic) way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

掩于岁月 2024-11-14 08:36:04

有几种不同的方法可以做到这一点。例如,如果您的列表仅包含数字

>>> my_list = [1, 2, 3.25]
>>> all(isinstance(item, int) for item in my_list)
False

>>> other_list = range(3)
>>> all(isinstance(item, int) for item in other_list)
True
>>> 

无论如何,如果您的列表包含布尔值,此解决方案将无法按预期工作,正如 @merlin 所说:

>>> another_list = [1, 2,False]
>>> all(isinstance(item, int) for item in another_list)
True

如果您的列表包含布尔值,您应该使用 type 而不是 isinstance (它有点慢,但可以按您的预期工作):

>>> another_list = [1, 2, False]
>>> all(type(item) is int for item in another_list)
False
>>> last_list = [1, 2, 3]
>>> all(type(item) is int for item in last_list)
True

There are a few different ways to do it. For example, if your list includes only numbers:

>>> my_list = [1, 2, 3.25]
>>> all(isinstance(item, int) for item in my_list)
False

>>> other_list = range(3)
>>> all(isinstance(item, int) for item in other_list)
True
>>> 

Anyways, this solution doesn't work as expected if your list includes booleans, as remarked by @merlin:

>>> another_list = [1, 2,False]
>>> all(isinstance(item, int) for item in another_list)
True

If your list include booleans you should use type instead of isinstance (it' a little slower, but works as you expect):

>>> another_list = [1, 2, False]
>>> all(type(item) is int for item in another_list)
False
>>> last_list = [1, 2, 3]
>>> all(type(item) is int for item in last_list)
True
倾听心声的旋律 2024-11-14 08:36:04

以下语句应该有效。它使用 any 内置函数和生成器表达式:

any(not isinstance(x, int) for x in l)

如果列表中存在非 int,则返回 true。例如:

>>> any(not isinstance(x, int) for x in [0,12.])
True
>>> any(not isinstance(x, int) for x in [0,12])
False

all 内置函数也可以完成相同的任务,有些人可能会认为它更有意义(请参阅 德拉甘答案

all(isinstance(x,int) for x in l)

The following statement should work. It uses the any builtin and a generator expression:

any(not isinstance(x, int) for x in l)

This will return true if there is a non-int in the list. E.g.:

>>> any(not isinstance(x, int) for x in [0,12.])
True
>>> any(not isinstance(x, int) for x in [0,12])
False

The all builtin could also accomplish the same task, and some might argue it is makes slightly more sense (see Dragan's answer)

all(isinstance(x,int) for x in l)
遥远的绿洲 2024-11-14 08:36:04

一种方法不是测试,而是坚持。这意味着你的程序可以智能地处理更广泛的输入——如果有人向它传递一个浮点数,它就不会失败。

int_list = [int(x) for x in int_list]

或(就地):

for i, n in enumerate(int_list):
    int_list[i] = int(n)

如果某些内容无法转换,它将引发异常,如果您愿意,您可以捕获该异常。

One approach would not be to test, but to insist. This means your program can handle a broader range of inputs intelligently -- it won't fail if someone passes it a float instead.

int_list = [int(x) for x in int_list]

or (in-place):

for i, n in enumerate(int_list):
    int_list[i] = int(n)

If something can't be converted, it will throw an exception, which you can then catch if you care to.

抠脚大汉 2024-11-14 08:36:04
In [1]: a = [1,2,3]

In [2]: all(type(item)==int for item in a)
Out[2]: True
In [1]: a = [1,2,3]

In [2]: all(type(item)==int for item in a)
Out[2]: True
流年已逝 2024-11-14 08:36:04

查看函数

def is_int(x):
    if type(x) == int:
        return True
    return


def all_int(a):
    for i in a:
        if not is_int(i):
            return False
    return True

然后调用

all_int(my_list) # returns boolean

See functions

def is_int(x):
    if type(x) == int:
        return True
    return


def all_int(a):
    for i in a:
        if not is_int(i):
            return False
    return True

Then call

all_int(my_list) # returns boolean
二手情话 2024-11-14 08:36:04

发现自己有同样的问题,但在不同的情况下:如果列表中的“整数”表示为字符串(例如,在一行整数和字符串上使用“line.split()”后我的情况就是这样读取文本文件),并且您只想检查列表的元素是否可以表示为整数,您可以使用:

all(i.isdigit() for i in myList)

例如:

>>> myList=['1', '2', '3', '150', '500', '6']
>>> all(i.isdigit() for i in myList)
True

>>> myList2=['1.5', '2', '3', '150', '500', '6']
>>> all(i.isdigit() for i in myList2)
False

Found myself with the same question but under a different situation: If the "integers" in your list are represented as strings (e.g., as was the case for me after using 'line.split()' on a line of integers and strings while reading in a text file), and you simply want to check if the elements of the list can be represented as integers, you can use:

all(i.isdigit() for i in myList)

For example:

>>> myList=['1', '2', '3', '150', '500', '6']
>>> all(i.isdigit() for i in myList)
True

>>> myList2=['1.5', '2', '3', '150', '500', '6']
>>> all(i.isdigit() for i in myList2)
False
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文