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

歡迎訪問 生活随笔!

生活随笔

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

python

python list_Python中的基本list操作

發布時間:2025/3/21 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python list_Python中的基本list操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 #coding=utf-8

2

3 #Filename : list.py

5 #Date: 2012 11 20

6

7

8

9 #創建一個list方式

10 heatList = ['wade','james','bosh','haslem']11 tableList = list('123') #list方法接受一個iterable的參數

12

13 print 'Miami heat has',len(heatList),'NBA Stars , they are:'

14

15 #遍歷list中的元素

16 for player inheatList:17 printplayer,18

19

20 #向list添加元素

21 heatList.append('allen') #方式一:向list結尾添加 參數object

22 print '\nAfter allen join the team ,they are:'

23 printheatList24

25 heatList.insert(4,'lewis') #方式二:插入一個元素 參數一:index位置 參數二:object

26 print 'After lewis join the team, they are:'

27 printheatList28

29 heatList.extend(tableList) #方式三:擴展列表,參數:iterable參數

30 print 'After extend a table list,now they are :'

31 printheatList32

33 #從list刪除元素

34 heatList.remove('1') #刪除方式一:參數object 如有重復元素,只會刪除最靠前的

35 print"Remove '1' ..now '1' is gone\n",heatList36

37 heatList.pop() #刪除方式二:pop 可選參數index刪除指定位置的元素 默認為最后一個元素

38 print "Pop the last element '3'\n",heatList39

40 del heatList[6] #刪除方式三:可以刪除制定元素或者列表切片

41 print "del '3' at the index 6\n",heatList42

43

44 #邏輯判斷

45

46 #統計方法 count 參數:具體元素的值

47 print 'james apears',heatList.count('wade'),'times'

48

49 #in 和 not in

50 print 'wade in list ?',('wade' inheatList)51 print 'wade not in list ?',('wade' not inheatList)52

53 #定位 index方法:參數:具體元素的值 可選參數:切片范圍

54 print 'allen in the list ?',heatList.index('allen')55 #下一行代碼會報錯,因為allen不在前三名里

56 #print 'allen in the fisrt 3 player ? ',heatList.index('allen',0,3)

57

58 #排序和反轉代碼

59 print 'When the list is reversed :'

60 heatList.reverse()61 printheatList62

63 print 'When the list is sorted:'

64 heatList.sort() #sort有三個默認參數 cmp=None,key=None,reverse=False 因此可以制定排序參數以后再講

65 printheatList66

67 #list 的分片[start:end] 分片中不包含end位置的元素

68 print 'elements from 2nd to 3rd' , heatList[1:3]

總結

以上是生活随笔為你收集整理的python list_Python中的基本list操作的全部內容,希望文章能夠幫你解決所遇到的問題。

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