python容器


1.List:

>>>L=[1,2,3,4]

1.修改列表:給元素賦值

>>>L[0]=2  L=[2,2,3,4,]

2.刪除元素

>>>del L[0]  L=[2,3,4]

3.給切片賦值

>>>L=list(‘Joker’)  L=['J', 'o', 'k', 'e', 'r']

列表方法

>>>L=[1,2,3,4]

1.append  方法append用於將一個對象附加到列表末尾。

>>>L.append(5)  >>>L  [1,2,3,4,5]

2.clear  方法clear就地清空列表的內容。

>>>L.clear()  >>>L   []

3.copy  方法 copy 復制列表。前面說過,常規復制只是將另一個名稱關聯到列表。

>>>L2=L.copy()  >>>L2   [1,2,3,4]

4.count  方法count計算指定的元素在列表中出現了多少次.

>>>L.count(1)  1

5.extend  方法extend讓你能夠同時將多個值附加到列表末尾,為此可將這些值組成的序列作為參數提供給方法extend。

>>>L2=[5,6,7]  >>>L.extend(L2)  [1,2,3,4,5,6,7]

6.index  方法index在列表中查找指定值第一次出現的索引。

>>>L.index(1)  0

7.insert  方法insert用於將一個對象插入列表。

>>>L.insert(0,0)  >>>L   [0,1,2,3,4]

8.pop  方法pop從列表中刪除一個元素(末尾為最后一個元素),並返回這一元素。

>>>L.pop()  4  >>>L  [1,2,3]

9.remove  方法remove用於刪除第一個為指定值的元素。

>>>L.remove(2)  >>>L  [1,3,4]

10.reverse  方法reverse按相反的順序排列列表中的元素。

>>>L.reverse()  >>>L  [4,3,2,1]

11.sort  方法sort用於對列表就地排序。

>>>L.sort()  >>>L  [1,2,3,4]

12.高級排序  方法sort接受兩個可選參數:key和reverse。

>>> x = ['aardvark', 'abalone', 'acme', 'add', 'aerate']

>>> x.sort(key=len)

>>> x

['add', 'acme', 'aerate', 'abalone', 'aardvark']

>>> x = [4, 6, 2, 1, 7, 9]

>>> x.sort(reverse=True)

>>> x

[9, 7, 6, 4, 2, 1]

2.tuple(不可改變的系列)

1.index

2.count

3.Dict

1.clear  方法clear刪除所有的字典項

>>>d ={'age': 42, 'name': 'Gumby'}  

>>>d.clear()  

{}

2.copy  方法copy返回一個新字典,其包含的鍵-值對與原來的字典相同

>>> x = {'username': 'admin', 'machines': ['foo', 'bar', 'baz']}

>>> y = x.copy()

>>> y['username'] = 'mlh'

>>> y['machines'].remove('bar')

>>> y

{'username': 'mlh', 'machines': ['foo', 'baz']}

>>> x

{'username': 'admin', 'machines': ['foo', 'baz']}

Deepcopy

>>> from copy import deepcopy

>>> d = {}

>>> d['names'] = ['Alfred', 'Bertrand']

>>> c = d.copy()

>>> dc = deepcopy(d)

>>> d['names'].append('Clive')

>>> c

{'names': ['Alfred', 'Bertrand', 'Clive']}

>>> dc

{'names': ['Alfred', 'Bertrand']}

3.fromkeys  方法fromkeys創建一個新字典,其中包含指定的鍵,且每個鍵對應的值都是None。

>>> dict.fromkeys(['name', 'age'])

{'age': None, 'name': None}

4.get  方法get為訪問字典項提供了寬松的環境。通常,如果你試圖訪問字典中沒有的項,將引發錯誤

>>> d = {}

>>> print(d.get('name'))

None

5.items  方法items返回一個包含所有字典項的列表,其中每個元素都為(key, value)的形式。字典項在列表中的排列順序不確定。

>>> d = {'title': 'Python Web Site', 'url': 'http://www.python.org', 'spam': 0}

>>> d.items()

dict_items([('url', 'http://www.python.org'), ('spam', 0), ('title', 'Python Web Site')])

6. keys  方法keys返回一個字典視圖,其中包含指定字典中的鍵。

7. pop  方法pop可用於獲取與指定鍵相關聯的值,並將該鍵-值對從字典中刪除。

>>> d = {'x': 1, 'y': 2}

>>> d.pop('x')

1

>>> d

{'y': 2}

8.popitem  方法popitem類似於list.pop,但list.pop彈出列表中的最后一個元素,而popitem隨機地彈出一個字典項,因為字典項的順序是不確定的.

>>> d = {'url': 'http://www.python.org', 'spam': 0, 'title': 'Python Web Site'}

>>> d.popitem()

('url', 'http://www.python.org')

>>> d

{'spam': 0, 'title': 'Python Web Site'}

9.setdefault  方法setdefault有點像get,因為它也獲取與指定鍵相關聯的值,但除此之外,setdefault還在字典不包含指定的鍵時,在字典中添加指定的鍵-值對。

>>> d = {}

>>> d.setdefault('name', 'N/A')

'N/A'

>>> d

{'name': 'N/A'}

>>> d['name'] = 'Gumby'

>>> d.setdefault('name', 'N/A')

'Gumby'

>>> d

{'name': 'Gumby'}

10.update  方法update使用一個字典中的項來更新另一個字典。

>>> d = {

... 'title': 'Python Web Site',

... 'url': 'http://www.python.org',

... 'changed': 'Mar 14 22:09:15 MET 2016'

... }

>>> x = {'title': 'Python Language Website'}

>>> d.update(x)

>>> d

{'url': 'http://www.python.org', 'changed':

'Mar 14 22:09:15 MET 2016', 'title': 'Python Language Website'}

11.values  方法values返回一個由字典中的值組成的字典視圖。不同於方法keys,方法values返回的視圖可能包含重復的值。

>>> d = {}

>>> d[1] = 1

>>> d[2] = 2

>>> d[3] = 3

>>> d[4] = 1

>>> d.values()

dict_values([1, 2, 3, 1])

 4.Str(不可改變)

所有標准序列操作(索引、切片、乘法、成員資格檢查、長度、最小值和最大值)都適用於字符串。

1.center

方法center通過在兩邊添加填充字符(默認為空格)讓字符串居中。

>>> "The Middle by Jimmy Eat World".center(39)

' The Middle by Jimmy Eat World '

>>> "The Middle by Jimmy Eat World".center(39, "*")

'*****The Middle by Jimmy Eat World*****'

2.find

方法find在字符串中查找子串。如果找到,就返回子串的第一個字符的索引,否則返回-1。

>>> title = "Monty Python's Flying Circus"

>>> title.find('Monty')

0

3.join

join是一個非常重要的字符串方法,其作用與split相反,用於合並序列的元素。

>>> seq = ['1', '2', '3', '4', '5']

>>>sep='+'

 

>>> sep.join(seq) # 合並一個字符串列表

'1+2+3+4+5'

4.lower(upper,title)

方法lower返回字符串的小寫版本。

>>> 'Trondheim Hammer Dance'.lower()

'trondheim hammer dance'

 

5.replace

方法replace將指定子串都替換為另一個字符串,並返回替換后的結果。

>>> 'This is a test'.replace('is', 'eez')

'Theez eez a test'

6.split

split是一個非常重要的字符串方法,其作用與join相反,用於將字符串拆分為序列。

>>> '1+2+3+4+5'.split('+')

['1', '2', '3', '4', '5']

>>> '/usr/bin/env'.split('/')

['', 'usr', 'bin', 'env']

>>> 'Using the default'.split()

['Using', 'the', 'default']

7.strip

方法strip將字符串開頭和末尾的空白(但不包括中間的空白)刪除,並返回刪除后的結果。

>>> ' internal whitespace is kept '.strip()

'internal whitespace is kept'

8.translate

方法translate與replace一樣替換字符串的特定部分,但不同的是它只能進行單字符替換。這個方法的優勢在於能夠同時替換多個字符,因此效率比replace高。

>>> table = str.maketrans('cs', 'kz', ' ')

>>> 'this is an incredible test'.translate(table)

'thizizaninkredibletezt'

9.判斷字符串是否滿足特定的條件

很多字符串方法都以is打頭,如isspace、isdigit和isupper,它們判斷字符串是否具有特定

的性質(如包含的字符全為空白、數字或大寫)。如果字符串具備特定的性質,這些方法就返回

True,否則返回False。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM