简体   繁体   中英

How to iterate through a list of tuples and checking each list for a specific order

I need some help in determining how to get the relevant numbers inside a list of tuples. I cannot figure out how to structure the nested statements to get the required results.

To explain it better:

I have a list of tuples as follows and they are all ready sorted smallest value to largest value:

my_list = [(1,2,3,4,5),(1,2,3,4,6),(1,2,3,4,7),(2,3,4,5,6),(2,3,4,5,7)]

So I need to iterate through each value inside each tuple and print the tuple if the values inside the tuple follows the previous number in the same tuple.

So the list that should be printed is [(1,2,3,4,5),(2,3,4,5,6)] as the numbers follow on each other where as the rest of the values in the other tuples jump a number from time to time. I will be assigning the new_list of numbers following each other in a new list and removing those numbers from a another list I have.

So the code I think should be as follows if someone can possibly shed some light on my problem or possibly make any suggestions:

new_list = []
for a in my_list: #iterate through the lists
  if i in a == ??(i-1) #iterate through each value in the list and check it against the number before it excluding the first iteration of i which will have no number before it.
      print(list if the numbers follow on each other)
      new_list.append(a)

You can do it with a couple of helper functions and a list comprehension:

# Find the difference between each consecutive element. Note skip the element at position 0 as there is nothing before it to 'diff' against
def diff(my_list):
     return [item - my_list[idx - 1] for idx, item in enumerate(my_list) if idx > 0]

# The items of a list are consecutive if all the elements of the diff list are 1
def is_consecutive(my_list):
     return all(item == 1 for item in diff(my_list))

# This is a basic filtering list comprehension based on the function we just defined
new_list = [my_tuple for my_tuple in my_list if is_consecutive(my_tuple)]

These sorts of operations are built into numpy, have you considered if it's worth your while using it?

Ok, so I was able to get the following, seems like I did not have to iterate through the tuple inside the list, instead I just called the index position and referenced it to the next index position - 1 as it is all int class:

Been struggling with it for days, couldn't believe it was so simple think it was more a problem about my brain being stuck to solve the problem than actually coding it out.

Please feel free to give me some suggestions to clean up the code a bit?

new_list = []
for a in my_list:
    if a[0] == a[1] -1 and a[1] == a[2] -1 and a[2] == a[3] -1 and a[3] == a[4] -1:
        new_list.append(a)

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