日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python:数组/列表(remove()函数、append()函数、sort()函数、reverse()函数)

發布時間:2025/7/25 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python:数组/列表(remove()函数、append()函数、sort()函数、reverse()函数) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

排序:

1:整理順序

#冒泡 lista = [5,7,11,19,99,63,3,9,1] list = [] while lista != []:number = 0for i in lista:if number < i:number = ilista.remove(number)list.append(number) print(list) 打印結果: D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py [99, 63, 19, 11, 9, 7, 5, 3, 1]Process finished with exit code 0
#選擇 lista = [5,7,11,19,99,63,3,9,1] list = [] while lista != []:for i in lista:number = 1for j in lista:if number < j:number = jlista.remove(number)list.append(number) print(list) 打印結果: D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py [99, 63, 19, 11, 9, 7, 5, 3, 1]Process finished with exit code 0

2:sort()函數

#sort函數 lista =[5,7,11,19,99,63,3,9,1] lista.sort() print(lista) 打印結果:
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py [1, 3, 5, 7, 9, 11, 19, 63, 99]Process finished with exit code 0

3:加上一個數

#若a=18,在原來的list中按順序加上這個個數 list = [1,4,7,9,11,14,19,34,60,79,98] print("前:%s" %list) a = 18 list.append(a) list_1 = [] while list != []:number = 100for i in list:if i < number:number = ilist.remove(number)list_1.append(number) print("后:%s" %list_1) 打印結果: D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py 前:[1, 4, 7, 9, 11, 14, 19, 34, 60, 79, 98] 后:[1, 4, 7, 9, 11, 14, 18, 19, 34, 60, 79, 98]Process finished with exit code 0

實例:

3*3表

#3*3表 listx = [1,2,3,] for i in listx:for j in listx:if i >= j:x = i*jprint("%s*%s=%s" %(i,j,x) ,end=" ")print() 打印結果: D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 Process finished with exit code 0

列表反向打印:list_num = ["1","2","3"]打印順序:3,2,1

使用len()函數

#反向列表(len() 方法返回對象(字符、列表、元組等)長度或項目個數) list_num = ["1","2","3"] i = len(list_num) print(list_num[i-1::-1]) 打印結果:
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py ['3', '2', '1']Process finished with exit code 0

使用reverse()函數

list_num = ["1","2","3"] list_num.reverse() print(list_num) 打印結果: D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py ['3', '2', '1']Process finished with exit code 0

使用for語句

a = [1,2,3,4,5,6] num = len(a) for i in range(int(num/2)):a[i],a[num -i -1 ] = a[num-i-1],a[i] print(a) D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py [6, 5, 4, 3, 2, 1]Process finished with exit code 0

小知識:

list_num = ["1","2","3"] for i in list_num[::-1]:#從后往前取值print(i) 打印結果: D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py 3 2 1Process finished with exit code 0

?

轉載于:https://www.cnblogs.com/dxxblog/p/9007367.html

總結

以上是生活随笔為你收集整理的python:数组/列表(remove()函数、append()函数、sort()函数、reverse()函数)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。