python - 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']
解决方案
我相信您需要两行代码,首先拆分值:
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']
推荐阅读
- python - NameError: name 'an1' is not defined ,我是编程新手,我想从测验中获得总分
- node.js - 如何使模型属性在环回 4 mongoDB 中唯一?
- r - 将虚拟编码矩阵转换为邻接矩阵
- javascript - 动态添加 select2 下拉菜单不起作用
- python - 以另一个顺序循环遍历二维数组
- paypal - 为什么有些 PayPal 测试 webhook 事件是空的?
- swift - 相当于 Haskell Show 的 Swift
- javascript - 点击动态创建 Vue 组件
- android - 带有 compileSdkVersion 28 的 flutter_map_location 显示错误
- php - 有没有办法通过 PHP 上传具有特定“tmp_name”的文件?