简体   繁体   中英

Merge the first elements in a list a tuples if the second elements are the same?

I have a list of tuples:

[('Donald', 'PERSON'), ('Trump', 'PERSON'), ('enters', 'O'), ('the', 'O'), ('White', 'LOCATION'), ('House', 'LOCATION')]

and the output I want is:

[('Donald Trump'), ('enters the'), ('White House')]

The below code gets me closer to the wanted result, but I'm not really familiar with the groupby function yet.

mergedTags = []
    from itertools import groupby
    for tag, chunk in groupby(tagList, lambda x: x[1]):
        if tag != "O":
            tagMerged = " ".join(w for w, t in chunk)
            mergedTags.extend([tagMerged])
        else:
            #tagMerged = " ".join(t for t, w in chunk)
            for word, chunk in groupby(tagList, lambda x: x[0]):
                mergedTags.extend([word])

    print(mergedTags)

You may use itertools.groupby with list comprehension expression as:

from itertools import groupby
my_list = [('Donald', 'PERSON'), ('Trump', 'PERSON'), ('enters', 'O'), ('the', 'O'), ('White', 'LOCATION'), ('House', 'LOCATION')]

output_list = [tuple(i[0] for i in e) for _, e in groupby(my_list, lambda x: x[1])]
#                 ^ generate the desired tuple

where the value hold by output_list will be:

[('Donald', 'Trump'), ('enters', 'the'), ('White', 'House')]

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