Type text and flip all odd words.
The only thing I thought of was:
s = input('Введите текст: ') print(s[::-1]) Please tell me how to do this?
We break the line into words, then we go around all the elements with the map function, the first argument is the lambda function, in which we reverse the odd words, the second argument is the counter, and the third is the list of words. Then restore the line:
input_str = input() words = input_str.split(' ') res = map(lambda i, w: w[::-1] if i % 2 != 0 else w, range(len(words)), words) print(' '.join(res)) It is possible without a map , but with the help of list inclusion, then you do not have to take the length, but use the enumerate function:
res = [w[::-1] if i % 2 != 0 else w for i, w in enumerate(words)] I would do it like this:
In [1]: words = input().split() 123 456 789 In [2]: words Out[2]: ['123', '456', '789'] In [3]: words[::2] = [w[::-1] for w in words[::2]] In [4]: words Out[4]: ['321', '456', '987'] Source: https://ru.stackoverflow.com/questions/596632/
All Articles