Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中反转子列表_Python_Python 3.x_List - Fatal编程技术网

如何在python中反转子列表

如何在python中反转子列表,python,python-3.x,list,Python,Python 3.x,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 有谁能帮我把它加入到实际的列表中,并带来预期的输出 输出=['aux ryi','bac rxo','lmn npx']我认为您需要两行代码,首先拆分值: b = [x.split() for x in a]

鉴于以下清单:

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

c = []
for i in a:
    x = i.split(" ")
    b= x[1][::-1] --- Got stuck after this line
有谁能帮我把它加入到实际的列表中,并带来预期的输出


输出=['aux ryi','bac rxo','lmn npx']

我认为您需要两行代码,首先拆分值:

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

[['aux', 'iyr'], ['bac', 'oxr'], ['lmn', 'xpn']]
['aux ryi', 'bac rxo', 'lmn npx']
然后恢复顺序:

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

[['aux', 'iyr'], ['bac', 'oxr'], ['lmn', 'xpn']]
['aux ryi', 'bac rxo', 'lmn npx']

您可以使用以下简单理解:

[" ".join((x, y[::-1])) for x, y in map(str.split, a)]
# ['aux ryi', 'bac rxo', 'lmn npx']

每个字符串是否有两个以上的令牌?如果是,第二个令牌之后会发生什么?