Home > Net >  How can I cut a list into a list of lists based on the presence of a particular string?
How can I cut a list into a list of lists based on the presence of a particular string?

Time:09-26

I'll try my best to explain.

Say I have this; it represents a username (ex: jjo), an optional real name (ex: josh) and it's always followed by a "remove".

list_of_people = ['jjo','josh','remove','flor30','florentina','remove','mary_h','remove','jasoncel3','jason celora','remove', 'lashit', 'remove']

My goal is to achieve this:

cut_list = [ ['jjo','josh'], ['flor30', 'florentina'], ['mary_h'], ['jasoncel3', 'jason celora'], ['lashit']]

The problem here is that the real name is optional and therefore, it's not always a perfect "trio". In other words, I need to use the presence of "remove" as a pivot to cut my list.

Verbally speaking, I would say that the code would be:

if you meet "remove", go backwards and store everything until you meet another "remove"

One issue is that there's no "remove" at the start (although I could manually add it), but my main issue is the logic. I can't get it right.

Here's my "best" shot so far and what it gives:

list_of_people = ['jjo','josh','remove','flor30','florentina','remove','mary_h','remove','jasoncel3','jason celora','remove', 'lashit', 'remove']

#Add the first 2 items
#If "remove" is there (means there was no real name), remove it
#Turn list into a list of lists
cut_list = list_of_people[0:2]

if "remove" in cut_list:
  cut_list.remove("remove")

cut_list = [cut_list]

#Loop through and cut based on the presence of "remove"
for i in range(2, len(list_of_people)):
  if list_of_people[i] == 'remove':
    first_back = list_of_people[i-1]
    if list_of_people.append(list_of_people[i-2]) != 'remove':
      second_back = list_of_people[i-2]
  
  cut_list.append([first_back, second_back])

print(cut_list)

# #Should give:
# ##cut_list = [ ['jjo','josh'], ['flor30', 'florentina'], ['mary_h'], ['jasoncel3', 'jason celora'], ['lashit']]

[['jjo', 'josh'], ['josh', 'jjo'], ['josh', 'jjo'], ['josh', 'jjo'], ['florentina', 'flor30'], ['florentina', 'flor30'], ['mary_h', 'remove'], ['mary_h', 'remove'], ['mary_h', 'remove'], ['jason celora', 'jasoncel3'], ['jason celora', 'jasoncel3'], ['lashit', 'remove']]

CodePudding user response:

I chose to keep this simple and iterate once through the list using the ”remove” as the marker to do additional processing.

list_of_people = ['jjo','josh','remove','flor30','florentina','remove','mary_h','remove','jasoncel3','jason celora','remove', 'lashit', 'remove']

result = []
people = []
for name in list_of_people:
    if name != "remove":
        # Add to the people list
        people.append(name)
    else:
        # Found a remove, reset people after adding to result
        result.append(people)
        people = []
        
print(result)

CodePudding user response:

from itertools import groupby

sentence = ['jjo', 'josh', 'remove', 'flor30', 'florentina', 'remove', 'mary_h',
            'remove', 'jasoncel3', 'jason celora', 'remove', 'lashit', 'remove']

i = (list(g) for _, g in groupby(sentence, key='remove'.__ne__))
l = [a   b for a, b in zip(i, i)]
N = 'remove'

res = [[ele for ele in sub if ele != N] for sub in l]
print(res)

CodePudding user response:

Try:

from itertools import groupby

out = [
    list(g) for v, g in groupby(list_of_people, lambda x: x != "remove") if v
]

print(out)

Prints:

[['jjo', 'josh'], ['flor30', 'florentina'], ['mary_h'], ['jasoncel3', 'jason celora'], ['lashit']]
  • Related