Innym rozwiązaniem mogłoby być -
import re
input_string = 'A3G3A'
alphabets = re.findall('[A-Z]', input_string) # List of all alphabets - ['A', 'G', 'A']
digits = re.findall('[0-9]+', input_string) # List of all numbers - ['3', '3']
final_output = "".join([alphabets[i]*int(digits[i]) for i in range(0, len(alphabets)-1)]) + alphabets[-1]
# This expression repeats each letter by the number next to it (Except for the last letter), joins the list of strings into a single string, and appends the last character
# final_output - 'AAAGGGA'
Objaśnienie -
In [31]: alphabets # List of alphabets in the string
Out[31]: ['A', 'G', 'A']
In [32]: digits # List of numbers in the string (Including numbers more than one digit)
Out[32]: ['3', '3']
In [33]: list_of_strings = [alphabets[i]*int(digits[i]) for i in range(0, len(alphabets)-1)] # List of strings after repetition
In [34]: list_of_strings
Out[34]: ['AAA', 'GGG']
In [35]: joined_string = "".join(list_of_strings) # Joined list of strings
In [36]: joined_string
Out[36]: 'AAAGGG'
In [38]: final_output = joined_string + input_string[-1] # Append last character of the string
In [39]: final_output
Out[39]: 'AAAGGGA'
[itertools.groupby] (https://docs.python.org/3/library/itertools .html # itertools.groupby) - wygeneruje iteratory zawierające {A, 10, G, 3, ABC}? w oparciu o true/false flip isdigit? –
@PatrickArtner, tak, ale będzie to również wynik 'isdigit()' wraz z iteratorami, więc wiesz również, na co patrzysz! Koncepcyjnie, coś w stylu: [[Fałsz, ['A']), (Prawda, ['1', '0']), (Fałsz, ['G']), (Prawda, ['3']) , (Fałsz, ['A', 'B', 'C'])] ' – cdlane
czysty, thx dla wyjaśnienia –