简体   繁体   中英

How to compare one item with two items at a time from a list

Sorry if this has been asked before, I have been looking for an answer for hours without success.

So, I have a number (it's actually a class object, but for example's sake it may be better to explain using integers): 1, and a list of unique numbers (my number not included).

Example: [2, 5, 3, 8, 9, 4...]

What I want to do is compare my number (1) with all numbers from the list, but two items/pair at a time, so the first ones here would be (2, 5). Then, with these three numbers (1, 2, 5) I can check if they meet a condition from a function of mine. If not, take my number again (1) and compare it with the next two items (3, 8) from the list and so on until the condition for all three numbers is met (or not).

Can you guys please help me out on how to achieve this? Thanks in advance.

for i in range(0, len(lst)-2, 2):
    tmp = lst[i:i+2].append(1)
    check(tmp)

You can just grab the elements separately and use it as arguments for you compare function, like that:

a = 1
b = [2,3,4,5,6,7]

for i in range(0, len(b), 2):
    print(a, b[i], b[i+1])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM