Home > database >  Finding consecutive groupings of items in list of lists in Python
Finding consecutive groupings of items in list of lists in Python

Time:04-05

I have a list of tuples like this:

my_list = [('Good', 'Y'), ('Shonen', 'Y'), ('anime', 'N'), ('similar', 'N'), ('to', 'N'), ('Demon', 'Y'), ('Slayer', 'Y'), ('or', 'N'), ('Hunter', 'Y'), ('X', 'Y'), ('Hunter', 'Y'), ('?', 'N')]

I'm trying to get a list of phrases from the tuples like this: ['Good Shonen', 'Demon Slayer', 'Hunter X Hunter']

with consecutive tup[1] == 'Y', skipping tuples otherwise, but I'm having hard time keeping track of where I am and what I've seen. What's a good way to approach this problem?

CodePudding user response:

you need to group the consecutive element based on value of 'Y' or 'N' and for value of 'Y' then join the first element to get the result

my_list = [('Good', 'Y'), ('Shonen', 'Y'), ('anime', 'N'), ('similar', 'N'), ('to', 'N'), ('Demon', 'Y'), ('Slayer', 'Y'), ('or', 'N'), ('Hunter', 'Y'), ('X', 'Y'), ('Hunter', 'Y'), ('?', 'N')]
from itertools import groupby

result = [ ' '.join(i[0] for i in b)for a, b in groupby(my_list, key=lambda x:x[1]) if a=='Y']
print(result)

# output
['Good Shonen', 'Demon Slayer', 'Hunter X Hunter']

CodePudding user response:

Try this:


my_list = [('Good', 'Y'), ('Shonen', 'Y'), ('anime', 'N'), ('similar', 'N'), ('to', 'N'), ('Demon', 'Y'), ('Slayer', 'Y'), ('or', 'N'), ('Hunter', 'Y'), ('X', 'Y'), ('Hunter', 'Y'), ('?', 'N')]

c = len(my_list)
result = []
ts = []
for val in my_list:
    if val[1] == 'Y':
        ts.append(val[0])
    else:
        if len(ts) > 0:
            t = ' '.join(ts)
            result.append(t)
            ts = []

        
print(result)

CodePudding user response:

You can try this code:

my_list = [('Good', 'Y'), ('Shonen', 'Y'), ('anime', 'N'), ('similar', 'N'), ('to', 'N'), ('Demon', 'Y'), ('Slayer', 'Y'), ('or', 'N'), ('Hunter', 'Y'), ('X', 'Y'), ('Hunter', 'Y'), ('?', 'N')]

z = []
for i in range(0, len(my_list)):
    # I used this because dictionaries would disregard the duplicate values.
    currentKey = my_list[i][0]
    currentValue = my_list[i][1]
    lastKey = my_list[i-1][0]
    lastValue = my_list[i-1][1]

    if len(z) == 0 and currentValue=='Y':
        z.append(currentKey)
    elif lastValue=='Y' and currentValue=='Y':
        z[-1]  = ' ' currentKey
    elif lastValue == 'N' and currentValue == 'Y':
        z.append(currentKey)
print(z)
  • Related