Consolidating Elements of a List [closed]

2024/11/24 3:11:30

I have a list of tuples:

[('fruit', 'O'), ('is', 'O'), ('the', 'O'), ('subject', 'O'), ('of', 'O'), ('a', 'O'), ('Roald', 'PERSON'), ('Dahl', 'PERSON'), ('children', 'O'), ("'s", 'O'), ('book', 'O'), ('?', 'O')]

I want to reduce this list to:

[('fruit', 'O'), ('is', 'O'), ('the', 'O'), ('subject', 'O'), ('of', 'O'), ('a', 'O'), ('Roald Dahl', PERSON'), ('children', 'O'), ("'s", 'O'), ('book', 'O'), ('?', 'O')]

That is, any consecutive tuples whose second value is not 'O' should have their first value concatenated. This should work for list of any length, and for any number of consecutive tuples to be joined.

Answer

You can use itertools.groupby to group utilizing the last element in each tuple:

import itertools
s = [(1, 2), (3, 4), (5, 4), (10, 4), (7, 8)]
s = [(a, list(b)) for a, b in itertools.groupby(s, key=lambda x:x[-1])]
final_s = [(sum(i[0] for i in b), a) for a, b in s] 

Output:

[(1, 2), (18, 4), (7, 8)]

Edit: regarding your new, non numeric list of tuples, you can try this:

from functools import reduce
def remove(data, to_avoid='O'):s = [(a, list(b)) for a, b in itertools.groupby(data, key=lambda x:x[-1])]final_s = [x for i in [b if a == to_avoid else [(reduce(lambda c, d: "{} {}".format(c, d), [h[0] for h in b]), a)] for a, b in s] for x in i]return final_s>>remove([('fruit', 'O'), ('is', 'O'), ('the', 'O'), ('subject', 'O'), ('of', 'O'), ('a', 'O'), ('Roald', 'PERSON'), ('Dahl', 'PERSON'), ('children', 'O'), ("'s", 'O'), ('book', 'O'), ('?', 'O')])

Output:

[('fruit', 'O'), ('is', 'O'), ('the', 'O'), ('subject', 'O'), ('of', 'O'), ('a', 'O'), ('Roald Dahl', 'PERSON'), ('children', 'O'), ("'s", 'O'), ('book', 'O'), ('?', 'O')]

For those of us less comprehension aware, and using operator.itemgetter instead of the lamda's

import itertools, operator
item0 = operator.itemgetter(0)
item1 = operator.itemgetter(1)
result = []
for k, g in itertools.groupby(s, key=item1):if k != 'O':result.append((' '.join(map(item0, g)),k))else:result.extend(g)
https://en.xdnf.cn/q/120589.html

Related Q&A

html textarea via python using post function

<div><textarea cols="25" rows="12" name="box" id="box" tabindex="2">TEXT GOES HERE </textarea></div><div><p class=&quo…

Fibonacci in Python - Simple solution [duplicate]

This question already has answers here:How to write the Fibonacci Sequence?(67 answers)Closed 6 years ago.n1 = 1 n2 = 1 n3 = n1 + n2 for i in range(10):n1 + n2print(n3)n1 = n2n2 = n3According to what …

snake game: snake colliding with itself

Hi I am currently writing a snake game code and I am nearly finished however I am having difficulty writing a code which will cause the game to end if the head of the snake collides with its body, I th…

python3 function not defined even though it is

when I try to call the changeProfile function, I keep getting the error "getAuthCode is not defined" even though it is clearly defined. Why do I keep getting this error? What am I doing wron…

Python maximum and minimum

Im supposed to write a function max_and_min that accepts a tuple containing integer elements as an argument and returns the largest and smallest integer within the tuple. The return value should be a t…

Get differences between two excel files

Problem SummaryGiven 2 excel files, each with 200 columns approx, and have a common index column - ie each row in both files would have a name property say, what would be the best to generate an output…

Most pythonic way to call a list of functions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 4…

Unable to get wanted output using for and range functions

For my homework assignment, I am using the for loop and the range function. I have to create a loop that printsHello 0 Hello 1 Hello 3 Hello 6 Hello 10The question says that the number corresponds to t…

i want to add a new field to an existing module odoo11 but i dont know why it didnt work

product_template.xmlthe view to add the customizing field to the product module<?xml version="1.0" encoding="utf-8"?><odoo><data><record id="product_temp…

How to check with more RegEx for one address in python using re.findall()

How to check with more RegEx for one address in python using re.findall()Ex: I want to apply the below regex rules # need to get addresstxt = "hello user 44 West 22nd Street, New York, NY 12345 fr…