我已经尝试过如下操作,可以使用列表理解来完成,但是要使用嵌套的for循环来完成,如下所示?
l=['temp','test']
l1=[]
for t in l:
for i in t:
l1.append(str(i[::-1]))
print(l1)
input: ['test','temp']
required output : ['pmet','tset']
for i in reversed(array):
print(i)
为了反转每个字符串,您可以使用[::-1]
,例如:
txt = "Hello World"[::-1] print(txt)
l=['temp','test']
l1=[]
for t in l:
l1.append(t[::-1])
print(l1)
l = ['temp', 'test']
l1 = [
word[::-1]
for word in l
]
print(l1)
输出:
['pmet', 'tset']
l=['temp','test']
print([''.join([w[i] for i in range(len(w)-1, -1, -1)]) for w in l])
输出:
['pmet', 'tset']