Home > Enterprise >  search of consecutive equal numbers in a list and add them to a new list - using pure python
search of consecutive equal numbers in a list and add them to a new list - using pure python

Time:07-24

I need to accomplish this task without any external libraries, without any, map or zip. For example:

lst = [1,1,1,2,2,3,4,1,2,2,2,3,3,1,1,1,1,1]

repeated_num = [1,2,2,3,1]

my code so far:

lst = input().split(',')
repeated_num = []
for i in range(len(lst)-1):
    if lst[i] == lst[i 1]:
         repeated_num.append(lst[i])
        
repeated_num

as you might see, this only works for 2 consecutive equal numbers. so a [2,2,2] would give [2,2], not good.

CodePudding user response:

Using a couple extra variables, you can keep track of the last number to appear, and how many times it has appeared in succession.

lst = [1,1,1,2,2,3,4,1,2,2,2,3,3,1,1,1,1,1]
repeated_num = []
last = lst[0]
count = 1
for x in range(1, len(lst)):
    if lst[x] == last:
        count  = 1
        if count == 2:
            repeated_num.append(last)
    else:
        last = lst[x]
        count = 1
print(repeated_num)

Output:

[1, 2, 2, 3, 1]

CodePudding user response:

You could do it like this (elements are added if it is equal to the next element and (it isn't the last element in the list containing repeats or it is different to the previous element)):

repeated = []
for i, j in enumerate(lst[:-1], 1):
    if j == lst[i] and (repeated[-1:] != [j] or j != lst[i-2]):
        repeated.append(j)

Output (with given input):

[1, 2, 2, 3, 1]

Or in Python 3.8 you can use the := operator and do it in a list comprehension:

n = None
[n:=j for i, j in enumerate(lst[:-1], 1) if j == lst[i] and (n != j or j != lst[i-2])]

CodePudding user response:

@Dominic inspired me, thanks!

repeated_num = []
lst = input().split(',')

for i in range(len(lst)):
    lst[i] = int(lst[i])

sizeoflist = len(lst)

currentnumber = 0
for i in range(sizeoflist - 1):
    if lst[i] == lst[i   1]:
        if currentnumber == 0:
            currentnumber = 1
            repeated_num.append(lst[i])
    else:
        currentnumber = 0

print(repeated_num)

CodePudding user response:

repeated_num = [[lst[0],1]]
for i in lst[1:]:
    if i == repeated_num[-1][0]:
        repeated_num[-1][1]  = 1
    else:
        repeated_num.append([i,1])
# strip and filter counts
repeated_num = [i[0] for i in repeated_num if i[1] > 1]

CodePudding user response:

Try this:

lst = [1,1,1,2,2,3,4,1,2,2,2,3,3,1,1,1,1,1]

repeated_numbers = []
counter = 0
for x in range(len(lst)-1):
    if repeated_numbers != [] and repeated_numbers[-1] != lst[x]:
        counter =0

    if counter == 0 and lst[x] == lst[x 1]:
        repeated_numbers.append(lst[x])
        counter  =1

CodePudding user response:

One approach using while instead of for, since for this case (in my opinion) it let's you handle iteration better:

lst = [1, 1, 1, 2, 2, 3, 4, 1, 2, 2, 2, 3, 3, 1, 1, 1, 1, 1]

res = []
length, index = len(lst), 0

while index < length:
    last = lst[index]

    # while the current value is equal to the last iterate
    run_length = 0
    while index   run_length < length and lst[index   run_length] == last:
        run_length  = 1

    #  if the run of consecutive values was greater than 1 append to result
    if run_length > 1:
        res.append(last)

    # move index forward
    index  = run_length

print(res)

Output

[1, 2, 2, 3, 1]

The idea is to increase the counter while there is run of consecutive numbers, then check if the run of consecutive numbers is larger than 1, if positive add on representative to the result.

CodePudding user response:

One-liner without :=:

lst = [1, 1, 1, 2, 2, 3, 4, 1, 2, 2, 2, 3, 3, 1, 1, 1, 1, 1]

out = [
    lst[i]
    for i in range(1, len(lst))
    if lst[i - 1] == lst[i] and (i == 1 or lst[i - 2] != lst[i - 1])
]

print(out)

Prints:

[1, 2, 2, 3, 1]
  • Related