[英]python- reverse 2 strings in 1 list separately?
In python, I have this list containing 在python中,我有这个包含的列表
['HELLO', 'WORLD']
how do I turn that list into 如何将该列表转换为
['OLLEH', 'DLROW']
>>> words = ['HELLO', 'WORLD']
>>> [word[::-1] for word in words]
['OLLEH', 'DLROW']
使用列表理解:
reversed_list = [x[::-1] for x in old_list]
Arguably using the builtin reversed
is more clear than slice notation x[::-1]
. 可以说使用内置reversed
比切片符号x[::-1]
更清晰。
[reversed(word) for word in words]
or 要么
map(reversed, words)
Map is fast without the lambda. 没有lambda,地图很快。
I just wish it was easier to get the string out of the resulting iterator. 我只是希望从生成的迭代器中获取字符串更容易。 Is there anything better than ''.join()
to put together the string from the iterator? 有没有什么比''.join()
更好地将迭代器中的字符串放在一起?
Using map
and lambda
(lambdas are slow): 使用map
和lambda
(lambdas很慢):
>>> lis=['HELLO', 'WORLD']
>>> map(lambda x:x[::-1],lis)
['OLLEH', 'DLROW']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.