Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

343
Visualizações
How to reduce a string by another string in Python?

I would like to remove all characters from a first string s1 exactly the number of times they appear in another string s2, i.e. if s1 = "AAABBBCCCCCCD" and s2 = "ABBCCC" then the result should be s = "AABCCCD". (The order of the characters in the resulting string is actually irrelevant but it's a plus if it can be preserved.)

The following rather crude code can do this:

def reduce_string(s1, s2):
    s = s1
    for c in s2:
        if c in s:
            s = s.replace(c, "", 1)
    return(s)

# examples
reduce_string("AAABBBCCCCCCD", "ABBCCC")
reduce_string("AAABBBCCCCCCD", "ABBCCCE")

My question is, can the same be achieved by clever use of some built-in function or at least in a more elegant way? Thank you for all your answers!

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can use counter objects. Subtract one against the other and join the remaining elements together.

from collections import Counter

s1 = "AAABBBCCCCCCD"
s2 = "ABBCCC"

counter = Counter(s1)
counter.subtract(Counter(s2))

result = ''.join(counter.elements())
print(result)
AABCCCD

As a one-liner:

print(''.join((Counter(s1) - Counter(s2)).elements()))
over 4 years ago · Santiago Trujillo Relatório

0

There is a filterfalse function in itertools module that you should see. Consult the documentation here.

The filterfalse function returns elements from a iterable when the predicate is evaluated as False.

So, one possible solution might be:

import itertools

def reduce_string(s1, s2):

    def predicate(letter, param=list(s2)):
        if letter in param:
            param.remove(letter)
            return True
        return False

    result = itertools.filterfalse(predicate, s1)

    return ''.join(result)

reduce_string("AAABBBCCCCCCD", "ABBCCC")
reduce_string("AAABBBCCCCCCD", "ABBCCCE")

However, notice how my predicate function is a little trickier when it changes the second string.

The keyword argument param is evaluated when the function predicate is created inside the reduce_string scope as a list object.

Since the reference does not change but the elements inside param, I was able to change the second string for comparisons reasons.

Now, the question remains: is there a more elegant way to define predicate function?

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda