Fernando Teixeira
Fernando Teixeira

Reputation: 45

How to find sequence of numbers in list

I am trying to find if some sequences of numbers are in a list. If so, this tuple should be removed from the list. Numbers must be contiguous to one another.

Example:

list of tuples:

list_1 =  [(), (1,), (3,), (2,), (4,), (1, 3), (1, 2), (1, 4), (3, 2), (3, 4), (2, 4), (1, 3, 2), (1, 3, 4), (1, 2, 4), (3, 2, 4), (1, 3, 2, 4)]

and a second list:

list_2 = [1,3,2,4]

In this case the elements (1, 3), (3, 2), (2, 4), (1, 3, 2), (3, 2, 4), (1, 3, 2, 4), (1, 3, 4), (1, 2, 4) in list_1 should be removed, because they contain numbers (not necessarily all of them!)that are contiguous in list_2.

I want to check if the elements in list

I tried something like this:

for i in range(len(lista)):
    teste = []
    for j in range(len(lista[i])):
        for k in stuff:
            if len(lista[i]) >= 2:
                if lista[i][j] == k:
                    teste.append(k)
                    if len(teste) == 2:
                        lista.remove(lista[i])
                        teste=[]

            else:
                pass

With this output:

if len(lista[i]) >= 2:
IndexError: list index out of range

I was trying to check a number, append it to a list check the next one and if it was appended too i would remove the corresponding tuple.

Upvotes: 2

Views: 518

Answers (3)

Diesel
Diesel

Reputation: 5335

Here you go, I think this works.

If you want to delete the blank set, just delete it, it's however you define a match. I added commented code to do that.

Fixed based on your comment

print("List 1:", list_1)
print("List 2:", list_2)
removeElements = []
for k in range(len(list_1)):
    #if len(list_1[k]) == 0:
    #    removeElements.append(k)
    count = 0
    for i in range(0, len(list_2)):
        for j in range(0, len(list_1[k])):
            if len(list_2) <= i + j:
                break
            count = count + 1
            if list_1[k][j] != list_2[i + j]:
                count = 0
            if count == 2:
                print("removing", list_1[k])
                removeElements.append(k)

removeElements = sorted(set(removeElements))
for i in range(len(removeElements)):
    list_1.pop(removeElements[i] - i)

print("List_1:", list_1)

Upvotes: 3

Joe T. Boka
Joe T. Boka

Reputation: 6589

This is one way to do it:

list_1 = [(), (1,), (1,2), (1,4)]
y = [list(i) for i in list_1]

list_2=[3,1,2,4]
x = [list_2[0:2], list_2[1:3], list_2[2:4]]

new_list_1 = [tuple(i) for i in y if i not in x[0:]]

Output:

[(), (1,), (1, 4)]

Upvotes: 0

Ryan Haining
Ryan Haining

Reputation: 36782

If you grab the window recipe from an old python example this becomes easy:

from itertools import islice

def window(seq, n=2):
    "Returns a sliding window (of width n) over data from the iterable"
    "   s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ...                   "
    it = iter(seq)
    result = tuple(islice(it, n))
    if len(result) == n:
        yield result    
    for elem in it:
        result = result[1:] + (elem,)
        yield result

Then you can just keep the elements that don't appear in any window from list_2

[t for t in list_1 if t not in window(list_2, len(t))]

However, you seem to have special cased tuples of zero or one element, so you'd need to change the filter

[t for t in list_1 if len(t) < 2 or t not in window(list_2, len(t))]

Upvotes: 0

Related Questions