python--列表,元組

來源:互聯網
上載者:User

一.概述

1.資料結構是通過某些方式組織在一起的資料元素的集合,這些元素可以是數字或者字元,也可以是其他資料結構;

2.最基本的資料結構是序列,序列中的每個元素被分配一個序號,即索引,從0開始;

3.Python中有六種內建的序列:列表,元組,字串,Unicode字串,buffer對象,xrange對象;

4.列表可以修改,元組則不能;

5.None是Python內內建的一個空值;

6.列表用[],元組用();

7.元組可以在映射(和集合的成員)中當做鍵使用,而列表不行。


二.基本操作

1.所有序列都可以進行某些特定的操作:索引(indexing)、分區(slicing)、加(adding)、乘(multiplying)、檢查某個元素是否屬於序列的成員(成員資格)、內建的max(),min(),len()函數;


2.索引:元素可以通過索引訪問,索引從0開始,最後一個的編號是-1;


3.分區:可以通過分區訪問一定範圍內的元素,還可以指定步長(步長不能為零,可以是負數,表示從右開始取),使用方法:lst[begin,end[,step]],注意:訪問的元素包括第一個索引,不包括第二個索引,也可以不指定第一個索引,這表示從第一個元素開始,如果不指定第二個索引,表示取到最後一個元素為止,如果都不指定,取出全部元素;

>>> a = list('helloworld')
>>> a
['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']
>>> a[:]
['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']
>>> a[:3]
['h', 'e', 'l']
>>> a[5:]
['w', 'o', 'r', 'l', 'd']
>>> a[::2]
['h', 'l', 'o', 'o', 'l']


4.序列相加:使用“+”可以實現序列串連的操作;

>>> a + list('!!')
['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '!', '!']


5.乘法:用“*”可以實現重複該序列n次;

>>> list('hello') * 2
['h', 'e', 'l', 'l', 'o', 'h', 'e', 'l', 'l', 'o']


6.成員資格:可以實現檢查一個元素是否在序列中,in操作,返回布爾值(True,False);

>>> a
['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']
>>> 'h' in a
True
>>> 'z ' in a
False


7.元素賦值:lst[index]=value;

>>> a
['h', 'e', 'a', 'l', 'l', 'o', 'abcd', 'a', 'b', 'c', 'd']
>>> a[2]='b'
>>> a
['h', 'e', 'b', 'l', 'l', 'o', 'abcd', 'a', 'b', 'c', 'd']


8.刪除元素:del lst[index];

>>> a
['h', 'e', 'b', 'l', 'l', 'o', 'abcd', 'a', 'b', 'c', 'd']
>>> del a[2]
>>> a
['h', 'e', 'l', 'l', 'o', 'abcd', 'a', 'b', 'c', 'd']



9.分區賦值:可以使用與原序列不等長的序列將分區替換,也可以在不需要替換任何原有元素的情況下插入新的元素,也可以刪除元素;

  >>> a
['h', 'e', 'l', 'l', 'o', 'abcd', 'a', 'b', 'c', 'd']
>>> a[5:]='a'
>>> a
['h', 'e', 'l', 'l', 'o', 'a']
>>> a[5:5]='b'
>>> a
['h', 'e', 'l', 'l', 'o', 'b', 'a']
>>> a[5:6]=[]
>>> a
['h', 'e', 'l', 'l', 'o', 'a']


三.函數

1.len(),max(),min():分別返回序列長度,最大值和最小值;

>>> a
['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']

>>> len(a)
10
>>> max(a)
'w'
>>> min(a)
'd'


2.list(seq):把序列轉換成列表;

>>> list('hello')
['h', 'e', 'l', 'l', 'o']


3.tuple(seq):把序列轉換成元組;

>>> tuple('hello')
('h', 'e', 'l', 'l', 'o')


4.reserved(seq):把序列進行反向迭代,返回迭代器對象;

>>> reversed(list('abcd'))
<listreverseiterator object at 0xb779ea8c>


5.sorted(seq):返回排序後的列表;


>>> sorted(list('adcb'))
['a', 'b', 'c', 'd']

四.列表方法(對象.方法(參數))

1.append()方法:在列表末尾追加元素,lst.append(value);

>>> a = list('hello')
>>> a
['h', 'e', 'l', 'l', 'o']
>>> a.append('abcd')
>>> a
['h', 'e', 'l', 'l', 'o', 'abcd']


2.count()方法:返回某個元素出現的次數,lst.count(value);

>>> a
['h', 'e', 'l', 'l', 'o', 'abcd']
>>> a.count('h')
1
>>> a.count('l')
2


3.extend()方法:可以在列表末尾一次性追加另一個序列中的多個值,結果返回被擴充的序列,lst.extend(seq);

>>> a.extend(list('abcd'))
>>> a
['h', 'e', 'l', 'l', 'o', 'abcd', 'a', 'b', 'c', 'd']


4.index()方法:返回從列表中找出某個值第一個匹配項的索引位置,lst.index(value);

>>> a.index('l')
2


5.insert()方法:用於將對象插入到列表中,a.insert(index,value);

>>> a.insert(2,'a')
>>> a
['h', 'e', 'a', 'l', 'l', 'o', 'abcd', 'a', 'b', 'c', 'd']


6.pop()方法:會移除列表最後一個元素,並返回該元素(pop方法是唯一一個既能修改列表又能返回元素值的列表方法),lt.pop([index]);

>>> a
['h', 'e', 'l', 'l', 'o']
>>> a.pop()
'o'
>>> a
['h', 'e', 'l', 'l']


7.remove()方法:會移除列表中第一個匹配的值,lst.remove(value);

>>> a.remove('l')
>>> a
['h', 'e', 'l']



8.reserve()方法:將列表的元素反向存放,lst.reverse();

>>> a.reverse()
>>> a
['l', 'e', 'h']


9.sort()方法:對列表進行排序,lst.sort([cmp][,kye][,reverse]);

>>> a.sort()
>>> a
['e', 'h', 'l']




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.