Python 中的list小結


list的下標和子list

     list的下表從零開始,和C語言挺類似的,但是增加了負下標的使用。

-len-----第一個元素

......       ......
-2 ------ 倒數第二個元素

-1 ------ 最后一個元素

0 ------ 第一個元素

len-1 ------ 最后一個元素

 

>>> a=[0,1,2,3,4,5,6,7]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7]
>>> i = -len(a)
>>> i
-8
>>> while i < len(a):
	print "a[",i,"]=",a[i]
	
SyntaxError: invalid syntax
>>> while i < len(a):
	"a[" , i , "]=" , a[i]
	i += 1

	
('a[', -8, ']=', 0)
('a[', -7, ']=', 1)
('a[', -6, ']=', 2)
('a[', -5, ']=', 3)
('a[', -4, ']=', 4)
('a[', -3, ']=', 5)
('a[', -2, ']=', 6)
('a[', -1, ']=', 7)
('a[', 0, ']=', 0)
('a[', 1, ']=', 1)
('a[', 2, ']=', 2)
('a[', 3, ']=', 3)
('a[', 4, ']=', 4)
('a[', 5, ']=', 5)
('a[', 6, ']=', 6)
('a[', 7, ']=', 7)

 

 

子list的提取:

        可通過下標指定范圍,用於提取出一個list的一部分。下標表明位置,一個是起始位置,一個是結束位置,中間使用冒號分割,如果不指定起始位置,則默認從0開始,如果不指定結束位置,結束位置為-1,子list表示包括起始位置處的元素,一直到結束位置,單數不包括結束位置的元素。負下標也可以參與下標的表示:

 

>>> a
[0, 1, 2, 3, 4, 5, 6, 7]
>>> b = a[1:4]
>>> b
[1, 2, 3]
>>> b = a[1:-1]
>>> b
[1, 2, 3, 4, 5, 6]
>>> b = a[1:]
>>> b
[1, 2, 3, 4, 5, 6, 7]
>>> b = a[:]
>>> b
[0, 1, 2, 3, 4, 5, 6, 7]
>>> 

改變新的子list的值,不會改變原來list的值

 


處理list的方法:

 

>>> #list.append(n),追加元素
>>> a
[0, 1, 2, 3, 4, 5, 6, 7]
>>> a.append(6)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 6]

 


>>> #list.count(var) , 計算var在list中出現的次數
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 6]
>>> a.count(6)
2
>>> 

 

>>> #len(list) , 返回list的長度
>>> len(a)
9
>>> #list.extend(list1) , 將list1追加到list的后面
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 6]
>>> b = [a,c,d,f,g]
Traceback (most recent call last):
  File "<pyshell#55>", line 1, in <module>
    b = [a,c,d,f,g]
NameError: name 'c' is not defined
>>> b = ['a' , 'b' , 'c' , 'd']
>>> b
['a', 'b', 'c', 'd']
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 6]
>>> a.extend(b)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 6, 'a', 'b', 'c', 'd']
>>> a.append(b)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 6, 'a', 'b', 'c', 'd', ['a', 'b', 'c', 'd']]
>>> 

 

>>> #list.index(var) , 返回var在list中的位置,若無,則拋出異常
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 6, 'a', 'b', 'c', 'd', ['a', 'b', 'c', 'd']]
>>> a.index('x')
Traceback (most recent call last):
  File "<pyshell#67>", line 1, in <module>
    a.index('x')
ValueError: 'x' is not in list
>>> a.index(4)
4
>>> a.index(6)
6
>>> 

 

>>> #list.insert(index,var) , 在index出插入var,其余元素向后推,如果index大於list的長度,就會在后面添加。如果index小於0,就要在最開始出添加
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 6, 'a', 'b', 'c', 'd', ['a', 'b', 'c', 'd']]
>>> a.insert(0,1)
>>> a
[1, 0, 1, 2, 3, 4, 5, 6, 7, 6, 'a', 'b', 'c', 'd', ['a', 'b', 'c', 'd']]
>>> a.insert(100,100)
>>> a
[1, 0, 1, 2, 3, 4, 5, 6, 7, 6, 'a', 'b', 'c', 'd', ['a', 'b', 'c', 'd'], 100]
>>> 

 

>>> #list.pop() , 返回最后一個元素,並且刪除最后一個元素。list.pop(index) , 返回index處的元素,並且刪除該元素。
>>> a
[1, 0, 1, 2, 3, 4, 5, 6, 7, 6, 'a', 'b', 'c', 'd', ['a', 'b', 'c', 'd'], 100]
>>> a.pop()
100
>>> a
[1, 0, 1, 2, 3, 4, 5, 6, 7, 6, 'a', 'b', 'c', 'd', ['a', 'b', 'c', 'd']]
>>> a.pop()
['a', 'b', 'c', 'd']
>>> a
[1, 0, 1, 2, 3, 4, 5, 6, 7, 6, 'a', 'b', 'c', 'd']
>>> a.pop(0)
1
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 6, 'a', 'b', 'c', 'd']
>>> 

 

>>> #list.remove(var) , 找到var並且刪除它,若無,則拋出異常
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 6, 'a', 'b', 'c', 'd']
>>> a.remove(9)
Traceback (most recent call last):
  File "<pyshell#98>", line 1, in <module>
    a.remove(9)
ValueError: list.remove(x): x not in list
>>> a.remove(6)
>>> a
[0, 1, 2, 3, 4, 5, 7, 6, 'a', 'b', 'c', 'd']
>>> #list.reverse() , 將list倒序
>>> a
[0, 1, 2, 3, 4, 5, 7, 6, 'a', 'b', 'c', 'd']
>>> a.reverse()
>>> a
['d', 'c', 'b', 'a', 6, 7, 5, 4, 3, 2, 1, 0]
>>> #list.sort() , 將list進行排序,a中元素若類型不同,結果自己看看一下,但是一般不會這么做
>>> a
['d', 'c', 'b', 'a', 6, 7, 5, 4, 3, 2, 1, 0]
>>> a.sort()
Traceback (most recent call last):
  File "<pyshell#107>", line 1, in <module>
    a.sort()
TypeError: unorderable types: int() < str()
>>> a = [1,3,2,4,5,6,3,2,1]
>>> a
[1, 3, 2, 4, 5, 6, 3, 2, 1]
>>> a.sort()
>>> a
[1, 1, 2, 2, 3, 3, 4, 5, 6]
>>> 



免責聲明!

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



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