Python常見數據結構整理
Python中常見的數據結構可以統稱爲容器(container)。序列(如列表和元組)、映射(如字典)以及集合(set)是三類主要的容器。
一、序列(列表、元組和字符串)
序列中的每個元素都有自己的編號。Python中有6種內建的序列。其中列表和元組是最常見的類型。其他包括字符串、Unicode字符串、buffer對象和xrange對象。下面重點介紹下列表、元組和字符串。
1、列表
列表是可變的,這是它區別於字符串和元組的最重要的特點,一句話概括即:列表可以修改,而字符串和元組不能。
(1)、創建
通過下面的方式即可創建一個列表:
1
2
3
4
|
list1 = [ 'hello' , 'world' ] print
list1 list2 = [ 1 , 2 , 3 ] print
list2 |
輸出:
['hello', 'world']
[1, 2, 3]
可以看到,這中創建方式非常類似於javascript中的數組。
(2)、list函數
通過list函數(其實list是一種類型而不是函數)對字符串創建列表非常有效:
1
2
|
list3 = list ( "hello" ) print
list3 |
輸出:
['h', 'e', 'l', 'l', 'o']
2、元組
元組與列表一樣,也是一種序列,唯一不同的是元組不能被修改(字符串其實也有這種特點)。
(1)、創建
1
2
3
4
5
6
|
t1 = 1 , 2 , 3 t2 = "jeffreyzhao" , "cnblogs" t3 = ( 1 , 2 , 3 , 4 ) t4 = () t5 = ( 1 ,) print
t1,t2,t3,t4,t5 |
輸出:
(1, 2, 3) ('jeffreyzhao', 'cnblogs') (1, 2, 3, 4) () (1,)
從上面我們可以分析得出:
a、逗號分隔一些值,元組自動創建完成;
b、元組大部分時候是通過圓括號括起來的;
c、空元組可以用沒有包含內容的圓括號來表示;
d、只含一個值的元組,必須加個逗號(,);
(2)、tuple函數
tuple函數和序列的list函數幾乎一樣:以一個序列(注意是序列)作爲參數並把它轉換爲元組。如果參數就算元組,那麼該參數就會原樣返回:
1
2
3
4
5
6
7
8
|
t1 = tuple ([ 1 , 2 , 3 ]) t2 = tuple ( "jeff" ) t3 = tuple (( 1 , 2 , 3 )) print
t1 print
t2 print
t3 t4 = tuple ( 123 ) print
t45 |
輸出:
(1, 2, 3)
('j', 'e', 'f', 'f')
(1, 2, 3)
Traceback (most recent call last):
File "F:\Python\test.py", line 7, in <module>
t4=tuple(123)
TypeError: 'int' object is not iterable
3、字符串
(1)創建
1
2
3
4
5
|
str1 = 'Hello world' print
str1 print
str1[ 0 ] for
c in str1: print
c |
輸出:
Hello world
H
H
e
l
l
o
w
o
r
l
d
(2)格式化
字符串格式化使用字符串格式化操作符即百分號%來實現。
1
2
|
str1 = 'Hello,%s'
% 'world.' print
str1 |
格式化操作符的右操作數可以是任何東西,如果是元組或者映射類型(如字典),那麼字符串格式化將會有所不同。
1
2
3
4
5
6
|
strs = ( 'Hello' , 'world' )
#元組 str1 = '%s,%s'
% strs print
str1 d = { 'h' : 'Hello' , 'w' : 'World' }
#字典 str1 = '%(h)s,%(w)s'
% d print
str1 |
輸出:
Hello,world
Hello,World
注意:如果需要轉換的元組作爲轉換表達式的一部分存在,那麼必須將它用圓括號括起來:
1
2
|
str1 = '%s,%s'
% 'Hello' , 'world' print
str1 |
輸出:
Traceback (most recent call last):
File "F:\Python\test.py", line 2, in <module>
str1='%s,%s' % 'Hello','world'
TypeError: not enough arguments for format string
如果需要輸出%這個特殊字符,毫無疑問,我們會想到轉義,但是Python中正確的處理方式如下:
1
2
|
str1 = '%s%%'
% 100 print
str1 |
輸出:100%
對數字進行格式化處理,通常需要控制輸出的寬度和精度:
1
2
3
4
5
6
7
|
from
math import pi str1 = '%.2f'
% pi #精度2 print
str1 str1 = '%10f'
% pi #字段寬10 print
str1 str1 = '%10.2f'
% pi #字段寬10,精度2 print
str1 |
輸出:
3.14
3.141593
3.14
字符串格式化還包含很多其他豐富的轉換類型,可參考官方文檔。
Python中在string模塊還提供另外一種格式化值的方法:模板字符串。它的工作方式類似於很多UNIX Shell裏的變量替換,如下所示:
1
2
3
4
|
from
string import Template str1 = Template( '$x,$y!' ) str1 = str1.substitute(x = 'Hello' ,y = 'world' ) print
str1 |
輸出:
Hello,world!
如果替換字段是單詞的一部分,那麼參數名稱就必須用括號括起來,從而準確指明結尾:
1
2
3
4
|
from
string import Template str1 = Template( 'Hello,w${x}d!' ) str1 = str1.substitute(x = 'orl' ) print
str1 |
輸出:
Hello,world!
如要輸出$符,可以使用$$輸出:
1
2
3
4
|
from
string import Template str1 = Template( '$x$$' ) str1 = str1.substitute(x = '100' ) print
str1 |
輸出:100$
除了關鍵字參數之外,模板字符串還可以使用字典變量提供鍵值對進行格式化:
1
2
3
4
5
|
from
string import Template d = { 'h' : 'Hello' , 'w' : 'world' } str1 = Template( '$h,$w!' ) str1 = str1.substitute(d) print
str1 |
輸出:
Hello,world!
除了格式化之外,Python字符串還內置了很多實用方法,可參考官方文檔,這裏不再列舉。
4、通用序列操作(方法)
從列表、元組以及字符串可以“抽象”出序列的一些公共通用方法(不是你想像中的CRUD),這些操作包括:索引(indexing)、分片(sliceing)、加(adding)、乘(multiplying)以及檢查某個元素是否屬於序列的成員。除此之外,還有計算序列長度、最大最小元素等內置函數。
(1)索引
1
2
3
4
5
6
|
str1 = 'Hello' nums = [ 1 , 2 , 3 , 4 ] t1 = ( 123 , 234 , 345 ) print
str1[ 0 ] print
nums[ 1 ] print
t1[ 2 ] |
輸出
H
2
345
索引從0(從左向右)開始,所有序列可通過這種方式進行索引。神奇的是,索引可以從最後一個位置(從右向左)開始,編號是-1:
1
2
3
4
5
6
|
str1 = 'Hello' nums = [ 1 , 2 , 3 , 4 ] t1 = ( 123 , 234 , 345 ) print
str1[ - 1 ] print
nums[ - 2 ] print
t1[ - 3 ] |
輸出:
o
3
123
(2)分片
分片操作用來訪問一定範圍內的元素。分片通過冒號相隔的兩個索引來實現:
1
2
3
4
5
6
7
8
|
nums = range ( 10 ) print
nums print
nums[ 1 : 5 ] print
nums[ 6 : 10 ] print
nums[ 1 :] print
nums[ - 3 : - 1 ] print
nums[ - 3 :]
#包括序列結尾的元素,置空最後一個索引 print
nums[:] #複製整個序列 |
輸出:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4]
[6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[7, 8]
[7, 8, 9]
不同的步長,有不同的輸出:
1
2
3
4
5
6
7
8
|
nums = range ( 10 ) print
nums print
nums[ 0 : 10 ]
#默認步長爲1 等價於nums[1:5:1] print
nums[ 0 : 10 : 2 ]
#步長爲2 print
nums[ 0 : 10 : 3 ]
#步長爲3 ##print nums[0:10:0] #步長爲0 print
nums[ 0 : 10 : - 2 ]
#步長爲-2 |
輸出:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 2, 4, 6, 8]
[0, 3, 6, 9]
[]
(3)序列相加
1
2
3
4
5
6
7
|
str1 = 'Hello' str2 = ' world' print
str1 + str2 num1 = [ 1 , 2 , 3 ] num2 = [ 2 , 3 , 4 ] print
num1 + num2 print
str1 + num1 |
輸出:
Hello world
[1, 2, 3, 2, 3, 4]
Traceback (most recent call last):
File "F:\Python\test.py", line 7, in <module>
print str1+num1
TypeError: cannot concatenate 'str' and 'list' objects
(4)乘法
1
2
3
4
5
6
|
print
[ None ] * 10 str1 = 'Hello' print
str1 * 2 num1 = [ 1 , 2 ] print
num1 * 2 print
str1 * num1 |
輸出:
[None, None, None, None, None, None, None, None, None, None]
HelloHello
[1, 2, 1, 2]
Traceback (most recent call last):
File "F:\Python\test.py", line 5, in <module>
print str1*num1
TypeError: can't multiply sequence by non-int of type 'list'
(5)成員資格
in運算符會用來檢查一個對象是否爲某個序列(或者其他類型)的成員(即元素):
1
2
3
4
5
|
str1 = 'Hello' print
'h' in str1
print
'H' in str1 num1 = [ 1 , 2 ] print
1 in num1 |
輸出:
False
True
True
(6)長度、最大最小值
通過內建函數len、max和min可以返回序列中所包含元素的數量、最大和最小元素。
1
2
3
4
5
6
7
8
|
str1 = 'Hello' print
len (str1) print
max (str1) print
min (str1) num1 = [ 1 , 2 , 1 , 4 , 123 ] print
len (num1) print
max (num1) print
min (num1) |
輸出:
5
o
H
5
123
1
二、映射(字典)
映射中的每個元素都有一個名字,如你所知,這個名字專業的名稱叫鍵。字典(也叫散列表)是Python中唯一內建的映射類型。
1、鍵類型
字典的鍵可以是數字、字符串或者是元組,鍵必須唯一。在Python中,數字、字符串和元組都被設計成不可變類型,而常見的列表以及集合(set)都是可變的,所以列表和集合不能作爲字典的鍵。鍵可以爲任何不可變類型,這正是Python中的字典最強大的地方。
1
2
3
4
5
6
7
8
|
list1 = [ "hello,world" ] set1 = set ([ 123 ]) d = {} d[ 1 ] = 1 print
d d[list1] = "Hello world." d[set1] = 123 print
d |
輸出:
{1: 1}
Traceback (most recent call last):
File "F:\Python\test.py", line 6, in <module>
d[list1]="Hello world."
TypeError: unhashable type: 'list'
2、自動添加
即使鍵在字典中並不存在,也可以爲它分配一個值,這樣字典就會建立新的項。
3、成員資格
表達式item in d(d爲字典)查找的是鍵(containskey),而不是值(containsvalue)。
Python字典強大之處還包括內置了很多常用操作方法,可參考官方文檔,這裏不再列舉。
思考:根據我們使用強類型語言的經驗,比如C#和Java,我們肯定會問Python中的字典是線程安全的嗎?
三、集合
集合(Set)在Python 2.3引入,通常使用較新版Python可直接創建,如下所示:
strs=set(['jeff','wong','cnblogs']) nums=set(range(10))
看上去,集合就是由序列(或者其他可迭代的對象)構建的。集合的幾個重要特點和方法如下:
1、副本是被忽略的
集合主要用於檢查成員資格,因此副本是被忽略的,如下示例所示,輸出的集合內容是一樣的。
1
2
3
4
5
|
set1 = set ([ 0 , 1 , 2 , 3 , 0 , 1 , 2 , 3 , 4 , 5 ]) print
set1 set2 = set ([ 0 , 1 , 2 , 3 , 4 , 5 ]) print
set2 |
輸出如下:
set([0, 1, 2, 3, 4, 5])
set([0, 1, 2, 3, 4, 5])
2、集合元素的順序是隨意的
這一點和字典非常像,可以簡單理解集合爲沒有value的字典。
1
2
|
strs = set ([ 'jeff' , 'wong' , 'cnblogs' ]) print
strs |
輸出如下:
set(['wong', 'cnblogs', 'jeff'])
3、集合常用方法
a、交集union
1
2
3
4
5
6
|
set1 = set ([ 1 , 2 , 3 ]) set2 = set ([ 2 , 3 , 4 ]) set3 = set1.union(set2) print
set1 print
set2 print
set3 |
輸出:
set([1, 2, 3])
set([2, 3, 4])
set([1, 2, 3, 4])
union操作返回兩個集合的並集,不改變原有集合。使用按位與(OR)運算符“|”可以得到一樣的結果:
1
2
3
4
5
6
|
set1 = set ([ 1 , 2 , 3 ]) set2 = set ([ 2 , 3 , 4 ]) set3 = set1|set2 print
set1 print
set2 print
set3 |
輸出和上面union操作一模一樣的結果。
其他常見操作包括&(交集),<=,>=,-,copy()等等,這裏不再列舉。
1
2
3
4
5
6
7
8
9
10
|
set1 = set ([ 1 , 2 , 3 ]) set2 = set ([ 2 , 3 , 4 ]) set3 = set1&set2 print
set1 print
set2 print
set3 print
set3.issubset(set1) set4 = set1.copy() print
set4 print
set4 is set1 |
輸出如下:
set([1, 2, 3])
set([2, 3, 4])
set([2, 3])
True
set([1, 2, 3])
False
b、add和remove
和序列添加和移除的方法非常類似,可參考官方文檔:
1
2
3
4
5
6
7
8
9
|
set1 = set ([ 1 ]) print
set1 set1.add( 2 ) print
set1 set1.remove( 2 ) print
set1 print
set1 print
29 in set1 set1.remove( 29 )
#移除不存在的項 |
輸出:
set([1])
set([1, 2])
set([1])
set([1])
False
Traceback (most recent call last):
File "F:\Python\test.py", line 9, in <module>
set1.remove(29) #移除不存在的項
KeyError: 29
4、frozenset
集合是可變的,所以不能用做字典的鍵。集合本身只能包含不可變值,所以也就不能包含其他集合:
1
2
3
|
set1 = set ([ 1 ]) set2 = set ([ 2 ]) set1.add(set2) |
輸出如下:
Traceback (most recent call last):
File "F:\Python\test.py", line 3, in <module>
set1.add(set2)
TypeError: unhashable type: 'set'
可以使用frozenset類型用於代表不可變(可散列)的集合:
1
2
3
4
|
set1 = set ([ 1 ]) set2 = set ([ 2 ]) set1.add( frozenset (set2)) print
set1 |
輸出:
set([1, frozenset([2])])