位置:首頁 > 軟件操作教程 > 編程開發(fā) > Python > 問題詳情

python應用操作——四種翻轉(zhuǎn)字符串/列表的方式

提問人:ylm發(fā)布時間:2020-09-29

翻轉(zhuǎn)列表本身

In [49]: testList = [1, 3, 5]

In [50]: testList.reverse()

In [51]: testListOut[51]: [5, 3, 1]

在一個循環(huán)中翻轉(zhuǎn)并迭代輸出

In [52]: for element in reversed([1,3,5]):

    ...:     print(element)

    ...:     

5

3

1

一行代碼翻轉(zhuǎn)字符串

In [53]: "Test Python"[::-1]Out[53]: 'nohtyP tseT'

使用切片翻轉(zhuǎn)列表

[1, 3, 5][::-1]

繼續(xù)查找其他問題的答案?

相關視頻回答
回復(0)
返回頂部