简体   繁体   中英

Finding consecutive groupings of items in list of lists in Python

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?

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']

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)

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)

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