Copy between strings inside Python lists

Asked

Viewed 84 times

0

The operation consists of comparing two lists of strings, detecting which items are common and placing them in a certain order in a new list.

The closest I got was:

nova = []

lista = ['a', 'b', 'c', 'd', 'e']

coluna = ['a', '1', '2', '4', 'b', '2', '4', '5', '6', 'c', '3', '3', '3', 'd', '3', '3', 'e', '1']

The goal is, from the matching items, to copy them to the next, copy, and so on, to form a list like this:

nova = ['a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'd','d', 'd', 'e', 'e']

That is, depending on the indexes that coincide between two lists, these will be copied until the next.

The closest I came to the answer was using a nested loop:

for item_lista in lista:

 for item_coluna in coluna:

   if(item_lista==item_coluna):
    
      nova.append(item_lista)

   else:
     nova.append(item_coluna)
   

A list returns, but the wrong one I intended:

nova=['a',
 '1',
 '2',
 '4',
 'b',
 '2',
 '4',
 '5',
 '6',
 'c',
 '3',
 '3',
 '3',
 'd',
 '3',
 '3',
 'e',
 '1',
 'a',
 '1',
 '2',
 '4',
 'b',
 '2',
 '4',
 '5',
 '6',
 'c',
 '3',
 '3',
 '3',
 'd',
 '3',
 '3',
 'e',
 '1',
 'a',
 '1',
 '2',
 '4',
 'b',
 '2',
 '4',
 '5',
 '6',
 'c',
 '3',
 '3',
 '3',
 'd',
 '3',
 '3',
 'e',
 '1',
 'a',
 '1',
 '2',
 '4',
 'b',
 '2',
 '4',
 '5',
 '6',
 'c',
 '3',
 '3',
 '3',
 'd',
 '3',
 '3',
 'e',
 '1',
 'a',
 '1',
 '2',
 '4',
 'b',
 '2',
 '4',
 '5',
 '6',
 'c',
 '3',
 '3',
 '3',
 'd',
 '3',
 '3',
 'e',
 '1']

3 answers

3

Use a variable l to save the character to be repeated and then iterate through the list elements coluna and one by one, in c and check if they are contained in the list lista:

  • if yes the c is contained in lista assign this character to the variable l and add the list nova.
  • if you don’t add l the list nova.

Example:

nova = []
lista = ['a', 'b', 'c', 'd', 'e']
coluna = ['a', '1', '2', '4', 'b', '2', '4', '5', '6', 'c', '3', '3', '3', 'd', '3', '3', 'e', '1']

l= coluna[0]

for c in coluna:  
  nova.append(l := c if c in lista else l)

print(nova)

Test the code on Repl.it

Or apply the same reasoning using list comprehension:

lista = ['a', 'b', 'c', 'd', 'e']
coluna = ['a', '1', '2', '4', 'b', '2', '4', '5', '6', 'c', '3', '3', '3', 'd', '3', '3', 'e', '1']

l=coluna[0]
nova = [l:= c if c in lista else l for c in coluna]

print(nova)

Test the code on Repli.it

Both codes result in:

['a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'd', 'd', 'd', 'e', 'e']

2

Case to lista = ['a', 'b', 'c', 'd', 'e'] is not mandatory

the following code repeats the letter found until it finds a new letter

nova = []
for x in coluna:

    if x.isnumeric():
        nova.append(letra)
    else:
        nova.append(x)
        letra = x
    
print(nova)

If mandatory...

nova = []
letra = ''
for x in coluna:
    
        if x.isnumeric():
            if letra in lista:
                nova.append(letra)
        else:
            if x in lista:
                nova.append(x)
                letra = x
    
print(nova)
  • 1

    The mandatory case solved my situation! Thank you very much!!!!

2

Recursively, you might want to play a little more:

lista = ['a', 'b', 'c', 'd', 'e']

coluna = ['a', '1', '2', '4', 'b', '2', '4', '5', '6', 'c', '3', '3', '3', 'd', '3', '3', 'e', '1']

nova = []


def agrupar(pos):
    if coluna[pos] in lista:
        nova.append(coluna[pos])
        agrupar(pos + 1)
    else:
        nova.append(nova[-1])
        if pos + 1 < len(coluna):
            agrupar(pos + 1)


agrupar(0)
print(nova)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.