python3筆記-列表,python3筆記列表

來源:互聯網
上載者:User

python3筆記-列表,python3筆記列表

列表去重的兩種方式:

 1 # 建立列表放資料 2 a =[1,2,1,4,2] 3 b=[1,3,4,3,1,3] 4  5 d=[] 6 for i in a: 7     if i not in d: 8         d.append(i) 9 print(d)10 11 #set 去重12 b=set(b)13 print(b)

 列表切片、翻轉列表:

>>> s = 'abcdefgh'>>> s[::-1]   # 可以視為翻轉操作'hgfedcba'>>> s[::2]   # 隔一個取一個元素的操作'aceg' 

列表排序:sorted()

即可以保留原列表,又能得到已經排序好的列表sorted()操作方法如下:

>>> a = [5,7,6,3,4,1,2]>>> b = sorted(a)>>> a[5, 7, 6, 3, 4, 1, 2]>>> b[1, 2, 3, 4, 5, 6, 7]

sorted()方法可以用在任何資料類型的序列中,返回的總是一個列表形式:

>>> sorted('iplaypython.com')['.', 'a', 'c', 'h', 'i', 'l', 'm', 'n', 'o', 'o', 'p', 'p', 't', 'y', 'y']

 

序列類型主要操作符:

seq[ind]:獲得下標為ind的元素
seq[ind1:ind2]:獲得下標從ind1到ind2之間的元素,不包含ind2的元素
seq1+seq2:串連序列1和序列2,這種方式不是最有效,extend:seq1.extend(seq2) print(seq1),如果是字串使用.join
obj in seq:判斷obj元素是否包含在seq中
boj not in seq:判斷obj元素是否不包含在seq中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.