python中查看列表元素及调整列表元素位置

 

>>> test1 = ["aaa","bbb","ccc","ddd"]
>>> type(test1)
<class 'list'>
>>> len(test1)
4
>>> test1[0]   ## 查看第一个元素
'aaa'
>>> test1[2]
'ccc'
>>> test1[-1]  ## 查看最后一个元素
'ddd'
>>> test1[-2]
'ccc'
>>> temp = test1[0]   ## 以下调整第一个元素和第四个元素位置
>>> test1[0] = test[3]
>>> test1[0] = test1[3]
>>> test1[3] = temp
>>> test1
['ddd', 'bbb', 'ccc', 'aaa']
>>> test1 = ["aaa","bbb","ccc","ddd"]
>>> test1
['aaa', 'bbb', 'ccc', 'ddd']
>>> test1[0],test1[3] = test1[3],test1[0]   ## 调整元素位置
>>> test1
['ddd', 'bbb', 'ccc', 'aaa']

 

posted @ 2020-12-18 18:39  小鲨鱼2018  阅读(3286)  评论(0编辑  收藏  举报