Answer the question
In order to leave comments, you need to log in
Can you help with "cb = lambda m: random.choice(m.group(1).split('|'))"?
Hello.
I found a code on the Internet that generates a unique text.
def rand_text(s):
import re, random
rgx = re.compile('\{([^{}]*)\}')
cb = lambda m: random.choice(m.group(1).split('|'))
while 1:
r = rgx.sub(cb, s)
if len(r) == len(s):
return r
s = r
if __name__ == '__main__':
print(rand_text(u'{Привет|Ку}, %username%. Как {дел{а|ишки}|сам|жизнь|поживает твоя мамаша}?'))
cb = lambda m: random.choice(m.group(1).split('|'))
Answer the question
In order to leave comments, you need to log in
Passed here:
rgx.sub(cb, s)
All regexp matches will be passed as an argument to this function.
cb = lambda m: \ # Это лямбда-функция с одним аргументом по имени m
random.choice( # результат функции вычисляется методом случайного выбора из
m.group(1) # текста (сопоставленного с первой скобкой регекспа)
.split('|') # разбитого по символу "|"
)
rgx = re.compile('\{([^{}]*)\}')
chunks of text within curly braces, and m.group(1) will match that chunk each time the lambda is called.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question