首页 > 解决方案 > How to reverse a sublist in python

问题描述

Given the following list:

a = ['aux iyr','bac oxr','lmn xpn']

c = []
for i in a:
    x = i.split(" ")
    b= x[1][::-1] --- Got stuck after this line

Can anyone help me how to join it to the actual list and bring the expected output

output = ['aux ryi','bac rxo','lmn npx']

标签: pythonpython-3.xlist

解决方案


我相信您需要两行代码,首先拆分值:

b = [x.split() for x in a]

返回:

[['aux', 'iyr'], ['bac', 'oxr'], ['lmn', 'xpn']]

然后恢复顺序:

output = [x[0] +' '+ x[1][::-1] for x in b]

返回:

['aux ryi', 'bac rxo', 'lmn npx']

推荐阅读