Concatenate list elements that fall between list elements of certain value

2024/11/16 9:27:33

Imagine I have the following list:

>>> mylist[('a', u'DT'),('Satisfactory', u'JJ'),('tracing', u'VBG'),('with', u'IN'),('a', u'DT'),('fairly', u'RB'),('persistent', u'JJ'),('with', u'IN')]

How do I concatenate list items that fall between elements that contain u'IN' or u'DT' and return only the concatenated elements i.e:

[('Satisfactory tracing'),('fairly persistent')]
Answer

Here is one which shall give you the desired result. Maybe you need to optimize it a bit.

my_list = ([('a', u'DT'),('Satisfactory', u'JJ'),('tracing', u'VBG'),('with', u'IN'),('a', u'DT'),('fairly', u'RB'),('persistent', u'JJ'),('with', u'IN')])sequence_enable = False
new_list = []
for i in my_list:if i[1] == 'DT' or i[1] == 'IN':if not sequence_enable: # Start reading values sequence_enable = Truetemp_str = []else: # stop reading valuesnew_list.append(' '.join(temp_str)) sequence_enable = Falsecontinueelse: # store valuesif sequence_enable:temp_str.append(i[0])print(new_list)
# output: ['Satisfactory tracing', 'fairly persistent']
https://en.xdnf.cn/q/118943.html

Related Q&A

How to create list from 100 elements to list of 10 [duplicate]

This question already has answers here:How to iterate over a list in chunks(40 answers)Closed 4 years ago.I have small brain fade today and I believe it will be faster to get hint here than wondering f…

how to make the width of image longer using animation in tkinter GUI?

I have this image and I want to make the width of this image longer till the end of the window of tkinter using animation but I havent got any proper way to achieving this task. any suggestions ?

Syntax error with if statement in python 2.7

Im having trouble with a "Syntax Error: invalid syntax" message in python code that keeps moving the goals posts on me. Heres the sample code:another_answer = int(raw_input("> "…

Python- Quicksort program in-place

I have a quicksort program here, but there seems to be a problem with the result. I think there must have been some issue in the areas highlighted below when referencing some values. Any suggestions?#…

Maximum recursion error in `__eq__` implementation

class Coordinate(object):def __init__(self,x,y):self.x = xself.y = ydef getX(self):# Getter method for a Coordinate objects x coordinate.# Getter methods are better practice than just accessing an attr…

python swig : ImportError wrong ELF class: ELFCLASS64

I am trying to interface from python to c++ code via swig. I get the following error , while trying to execute my script.File "./driver.py", line 4, in <module>from fixMessageSim import…

cnn news webscraper return empty [] without information

so i wrote this code for now: from urllib import request from bs4 import BeautifulSoup import requests import csv import reserch_term = input(What News are you looking for today? )url = fhttps://editi…

Why the code shows all the addition process?

Code: sum=0 for i in range(10,91):sum=sum+iprint(sum)When I wrote this code, the answer was Output: 10 21 33 46 60 75 91 108 126 145 165 186 208 231 255 280 306 333 361 390 420 451 483 516 550 585 621 …

Creating a list of keywords by scrolling through a dataframe (python)

I have a dataframe that looks like this: dataFrame = pd.DataFrame({Name: ((" Verbundmrtel , Compound Mortar , Malta per stucchi e per incollaggio "),(" StoLevell In Absolute , StoLeve…

How to click this button with python selenium

Im looking to click the button highlighted in the screenshot below; have tried with pyautogui but found results to be inconsistent so trying selenium instead.Im having trouble identifying the button to…