简体   繁体   English

如何检查Iterable中是否有任何奇数/偶数(例如列表/元组)?

[英]How to check if there's any odd/even numbers in an Iterable (e.g. list/tuple)?

Suppose we're checking if there's any odd numbers in a list . 假设我们正在检查是否有在任何的奇数list The most direct way is: 最直接的方法是:

def has_odd(L):
    for v in L:
        if v % 2 == 1:
            return True
    return False

The has_odd function checks if there's any odd numbers in a list , once an odd number is found, it returns True . has_odd功能检查,如果有在任何奇数list ,一旦奇数被发现,则返回True But this seems a bit verbose. 但这看起来有点冗长。 A more concise way using reduce is as follow: 使用reduce更简洁方法如下:

reduce(lambda res, v: res or bool(v), L, False)

But this will iterate through all elements and is unnecessary, because once an odd number is found the result is surely True . 但是这将遍历所有元素并且是不必要的,因为一旦找到奇数,结果肯定是True

So, are there any other ways to do this? 那么,有没有其他方法可以做到这一点?

You can use the any() function to reduce the verbosity: 您可以使用any()函数来减少详细程度:

>>> l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> any(n % 2 == 1 for n in l)
True
>>>

Note however, any() is pretty much the same as you had originally just generalized , so don't expect a speed improvement: 但请注意, any()与您最初推广的几乎相同 ,所以不要指望速度提升:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

first of all let's write small indicator function for "oddness" like 首先让我们为“奇怪”写下小指标功能

def is_odd(number):
    return number % 2

then we can write our indicator for "has at least one odd number" using any with imap / map 那么我们可以用imap / map any一个来编写“至少有一个奇数”的指标

  • Python 2.* Python 2. *

     from itertools import imap def has_odd_number(iterable): return any(imap(is_odd, iterable)) 
  • Python 3.* Python 3. *

     def has_odd_number(iterable): return any(map(is_odd, iterable)) 

or with generator expression 或与发电机表达

def has_odd_number(iterable):
    return any(is_odd(number) for number in iterable)

Examples: 例子:

>>> has_odd_number([0])
False
>>> has_odd_number([1])
True

Another way. 其他方式。 you can use not all() not all()使用not all()

>>> l = [2, 4, 5, 6, 7, 8, 9, 10]
>>> not all(n%2==1 for n in l)
True

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django 迁移错误:错误:“选择”必须是可迭代的(例如,列表或元组) - Django Migration Error: ERRORS: 'choices' must be an iterable (e.g., a list or tuple) 使用容器(例如元组或列表)进行 Numpy 切片 - Numpy slicing with container (e.g. tuple or list) 如何在偶数和奇数的两个列表中将数字与可迭代对象分开? - How to separate numbers from an iterable in two lists of even and odd numbers? 如何有条件地将生成器管道分成两个分支(例如奇数和偶数生成器)并同时使用它们? - How to conditionally split/fork a generator pipeline into two branches (e.g. odd and even generators) and consume them simultaneously? 我们如何 label 矩阵 plot 与预定义的标签列表(例如素数列表)? - How do we label a matrix plot with a predefined list of labels (e.g. a list of prime numbers)? 如何使用Python将电话号码列表(例如202.202.2020)格式化为(202)202-2020? - How to format a list of phone numbers e.g. 202.202.2020 into (202) 202-2020 using Python? 从可迭代对象(例如列表或集合)生成伪有序对列表的Python方法 - Pythonic way to generate a pseudo ordered pair list from an iterable (e.g. a list or set) 在给定条件奇数或偶数的情况下,如何对列表中的偶数或奇数求和? - How to sum even or odd numbers in a list given the condition odd or even? 如何检查是否是某个时间,例如下午 2:00 - How to check if it's a certain time like e.g. 2:00pm 在 PySimpleGUI 中创建 window 布局时出现错误“您的行不是可迭代的(例如列表)” - Error "Your row is not an iterable (e.g. a list)" when creating window layout in PySimpleGUI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM