简体   繁体   English

Python - 如何检查列表中的下几个元素是否符合某些语句

[英]Python - How to check if next few elements in list meet some statement

I have a huge list of values, and I want to search for pattern: if listItem == some value and next 10 items in list meet some statement, then do something.我有一个巨大的值列表,我想搜索模式:如果 listItem == some value 并且列表中的下 10 个项目满足某个语句,那么做一些事情。 How to do this to avoid long multiple if condition code like that?如果条件代码那样,如何避免长倍数?

for i in range (0, len(list)):
    if list[i] == someValue and list[i+1] !=someValue and list[i+2] !=someValue and [...] and list[i+10] !=someValue: 
        doSomething()

You can do it like this:你可以这样做:

for i in range(len(lst)):
    if lst[i] == someValue and all(x != someValue for x in lst[i+1:i+11]): 
        doSomething()

I changed the variable name from list to lst because you shopuld not overwrite the list builtin type and I also removed the 0 in the range fucntion as it is not needed.我将变量名称从list更改为lst因为您不会覆盖list内置类型,并且我还删除了range功能中的0 ,因为它不需要。

The all fucntion checks that every element in the iterable passed as argument evaluates to True , and our iterable is a sequence of bools with the next 10 elements checked against someValue . all函数检查作为参数传递的迭代中的每个元素的计算结果为True ,我们的迭代是一个布尔序列,接下来的 10 个元素根据someValue进行检查。

Another option would be using enumerate(...) to avoid having to get the value again from the list instead of range(len(...)) :另一种选择是使用enumerate(...)以避免再次从列表中获取值而不是range(len(...))

for i, value in enumerate(lst):
    if value == someValue and all(x != someValue for x in lst[i+1:i+11]): 
        doSomething()

And we could make it into a generator:我们可以把它变成一个生成器:

for i in (i for i, value in enumerate(lst) if value == someValue and all(x != someValue for x in lst[i+1:i+11])):
    doSomething()

@HeapOverflow suggested another way to check for that condition which is more readable (see his answer for further detail and drop a like) that conbined with the generator syntax would be: @HeapOverflow 建议了另一种检查该条件的方法,该方法与生成器语法相结合,该方法更具可读性(请参阅他的回答以获取更多详细信息并删除类似内容):

for i in (i for i, value in enumerate(lst) if value == someValue not in lst[i+1:i+11]):
    doSomething()

Using membership test and chaining :使用成员资格测试和链接

for i, value in enumerate(lst):
    if value == someValue not in lst[i+1:i+11]:
        doSomething()

Did you mean that you are searching for arbitrary values?您的意思是您正在搜索任意值吗?

target = [someValue1, someValue2, ... someValue11]
for i in range (len(lst) - 10):
    if lst[i:i+11] == target: 
        doSomething()

Try this:尝试这个:

for i in range (0, len(list)):
    if list[i] == someValue and all(x != someValue for x in list[i+1:i+10]): 
        doSomething()

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

相关问题 如何检查列表'a'中的元素是否符合列表'b'中的条件? - How to check if elements in list 'a' meet conditions in list 'b'? 列表中有 n 个元素,如何检查它们是否满足条件? - Having n elements in list, how can I check if they meet a condition? 如何在if语句python中检查可变长度列表的元素 - How to check elements of a list of variable length in an if statement python 如何检查列表(Python)中是否存在子列表的某些元素? - How do I check if some elements of sublist exists in list (Python)? 如何通过if语句中的某些和/或条件检出列表中的某些特定元素 - How is it possible to check out for some particular elements in a list by some and/or conditions in one if statement 在 Python 中加入列表的一些元素 - Join a few elements of the list in Python Python检查列表中的现有元素并找到下一个值 - Python check existing elements in list and find next value 何时以及如何检查 Python 子类是否符合规范 - When and how to check Python subclasses meet specification python - 使用循环满足某些条件后,如何在python中查询某个列表元素? - How do I query a certain list element in python after they meet some conditions using a loop? 如何替换列表中的某些元素? Python - How to replace some elements in a list? Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM