Home > Enterprise >  Finding if two items are next to each other in a list
Finding if two items are next to each other in a list

Time:09-23

So for the program im writing im supposed to find if two items are next to each other in a list for example if A is next to A in [ A,A,B,C,B,]. so far I have

p1_vals = ['A','A','B','C']

p1_h = 0

for i in range(len(p1_vals)-1):    
    if p1_vals[i] == p1_vals[i 1]:
        p1_h  = 1                    
print(p1_h)

what i have find is if two values are next to each other. The code basically needs to find if two letters are next to each other and count how many times they are next to each other. For example if the list is [A,A,A,B] then the code will count how many times A is next to A. I'm fairly new at coding and could really use some help.

CodePudding user response:

You can put a bool to check the existence of successive elements more than two.

p1_vals = ['A','A','A','A','B', 'B','C']

p1_h = 0
previous = False
for i in range(len(p1_vals)-1):    
    if p1_vals[i] == p1_vals[i 1] and not previous:
        p1_h  = 1                    
        previous = True
    elif p1_vals[i] == p1_vals[i 1]: previous = False
print(p1_h)

CodePudding user response:

I was to submit user1740577's solution but then I have another solution which I think is more beginner-friendly.

vals = [1, 1, 2, 3, 3, 3]
count = 0

for a, b in vals[:-1], vals[1:]:
    if a == b:
        count  = 1

print(count)

To make it more clear, vals[:-1] is [1, 1, 2, 3, 3] in this example, and vals[1:] would be [1, 2, 3, 3, 3]. Then, you will iterate over the two lists and count.

CodePudding user response:

This function returns the largest number of successive duplicates within a given list (array).

def count_successive_dups(array):
    if len(array) <= 1:
        return 0
    count = max_count = 1
    prior_char = array[0]
    for char in array[1:]:
        if char == prior_char:
            count  = 1
            if count > max_count:
                max_count = count
        elif count > 1:
            count = 1
        prior_char = char
    return max_count

CodePudding user response:

Try this:

def sum_neighbor(lst):
    if len(lst)==0 : return 0
    cnt, max_cnt = 0,0
    fchar = lst[0]
    for i in range(len(lst)):
        char = lst[i]
        if fchar != char: cnt=0; fchar=char
        try:
            if (lst[i] == lst[i-1]) or (lst[i] == lst[i 1]):
                cnt  =1
                if max_cnt < cnt:
                    max_cnt = cnt
        except IndexError:
            pass
    return max_cnt

Output:

>>> print(sum_neighbor(['A','A','A','B']))
3
>>> print(sum_neighbor(['A','A','B','C']))
2
>>> print(sum_neighbor(['A','C','B','B']))
2
>>> print(sum_neighbor([]))
0

Or you can use dictionary and count for all char and find max like below:

def sum_neighbor_v2(lst):
    dct = {}
    for i in range(len(lst)):
        try:
            if lst[i] == lst[i-1] or lst[i] == lst[i 1]:
                dct[lst[i]] = 1 dct.get(lst[i],0)
        except IndexError:
            pass
    return dct
    
dct = sum_neighbor_v2(['A','A','A','B','B','B','B','B','C','C'])
# dct <-> {'A': 3, 'B': 5, 'C': 2}

key_max = max(dct, key=dct.get)
# key_max <-> 'B'

print(dct[key_max])
# 5 <-> You want this

  • Related