
Python常見數(shù)據(jù)結(jié)構(gòu)詳解
本文詳細羅列歸納了Python常見數(shù)據(jù)結(jié)構(gòu),并附以實例加以說明,相信對讀者有一定的參考借鑒價值。
總體而言Python中常見的數(shù)據(jù)結(jié)構(gòu)可以統(tǒng)稱為容器(container)。而序列(如列表和元組)、映射(如字典)以及集合(set)是三類主要的容器。
一、序列(列表、元組和字符串)
序列中的每個元素都有自己的編號。Python中有6種內(nèi)建的序列。其中列表和元組是最常見的類型。其他包括字符串、Unicode字符串、buffer對象和xrange對象。下面重點介紹下列表、元組和字符串。
1、列表
列表是可變的,這是它區(qū)別于字符串和元組的最重要的特點,一句話概括即:列表可以修改,而字符串和元組不能。
(1)、創(chuàng)建
通過下面的方式即可創(chuàng)建一個列表:
list1=['hello','world']
print list1
list2=[1,2,3]
print list2
輸出:
['hello', 'world']
[1, 2, 3]
可以看到,這中創(chuàng)建方式非常類似于javascript中的數(shù)組。
(2)、list函數(shù)
通過list函數(shù)(其實list是一種類型而不是函數(shù))對字符串創(chuàng)建列表非常有效:
list3=list("hello")
print list3
輸出:
['h', 'e', 'l', 'l', 'o']
2、元組
元組與列表一樣,也是一種序列,唯一不同的是元組不能被修改(字符串其實也有這種特點)。
(1)、創(chuàng)建
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、逗號分隔一些值,元組自動創(chuàng)建完成;
b、元組大部分時候是通過圓括號括起來的;
c、空元組可以用沒有包含內(nèi)容的圓括號來表示;
d、只含一個值的元組,必須加個逗號(,);
(2)、tuple函數(shù)
tuple函數(shù)和序列的list函數(shù)幾乎一樣:以一個序列(注意是序列)作為參數(shù)并把它轉(zhuǎn)換為元組。如果參數(shù)就算元組,那么該參數(shù)就會原樣返回:
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)創(chuàng)建
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)格式化
字符串格式化使用字符串格式化操作符即百分號%來實現(xiàn)。
str1='Hello,%s' % 'world.'
print str1
格式化操作符的右操作數(shù)可以是任何東西,如果是元組或者映射類型(如字典),那么字符串格式化將會有所不同。
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
注意:如果需要轉(zhuǎn)換的元組作為轉(zhuǎn)換表達式的一部分存在,那么必須將它用圓括號括起來:
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
如果需要輸出%這個特殊字符,毫無疑問,我們會想到轉(zhuǎn)義,但是Python中正確的處理方式如下:
str1='%s%%' % 100
print str1
輸出:
100%
對數(shù)字進行格式化處理,通常需要控制輸出的寬度和精度:
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
字符串格式化還包含很多其他豐富的轉(zhuǎn)換類型,可參考官方文檔。
Python中在string模塊還提供另外一種格式化值的方法:模板字符串。它的工作方式類似于很多UNIX Shell里的變量替換,如下所示:
from string import Template
str1=Template('$x,$y!')
str1=str1.substitute(x='Hello',y='world')
print str1
輸出:
Hello,world!
如果替換字段是單詞的一部分,那么參數(shù)名稱就必須用括號括起來,從而準(zhǔn)確指明結(jié)尾:
from string import Template
str1=Template('Hello,w${x}d!')
str1=str1.substitute(x='orl')
print str1
輸出:
Hello,world!
如要輸出$符,可以使用$$輸出:
from string import Template
str1=Template('$x$$')
str1=str1.substitute(x='100')
print str1
輸出:
100$
除了關(guān)鍵字參數(shù)之外,模板字符串還可以使用字典變量提供鍵值對進行格式化:
from string import Template
d={'h':'Hello','w':'world'}
str1=Template('$h,$w!')
str1=str1.substitute(d)
print str1
輸出:
Hello,world!
除了格式化之外,Python字符串還內(nèi)置了很多實用方法,可參考官方文檔,這里不再列舉。
4、通用序列操作(方法)
從列表、元組以及字符串可以“抽象”出序列的一些公共通用方法(不是你想像中的CRUD),這些操作包括:索引(indexing)、分片(sliceing)、加(adding)、乘(multiplying)以及檢查某個元素是否屬于序列的成員。除此之外,還有計算序列長度、最大最小元素等內(nèi)置函數(shù)。
(1)索引
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:
str1='Hello'
nums=[1,2,3,4]
t1=(123,234,345)
print str1[-1]
print nums[-2]
print t1[-3]
輸出:
o
3
123
(2)分片
分片操作用來訪問一定范圍內(nèi)的元素。分片通過冒號相隔的兩個索引來實現(xiàn):
nums=range(10)
print nums
print nums[1:5]
print nums[6:10]
print nums[1:]
print nums[-3:-1]
print nums[-3:] #包括序列結(jié)尾的元素,置空最后一個索引
print nums[:] #復(fù)制整個序列
輸出:
[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]
不同的步長,有不同的輸出:
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)序列相加
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)乘法
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運算符會用來檢查一個對象是否為某個序列(或者其他類型)的成員(即元素):
str1='Hello'
print 'h' in str1
print 'H' in str1
num1=[1,2]
print 1 in num1
輸出:
False
True
True
(6)長度、最大最小值
通過內(nèi)建函數(shù)len、max和min可以返回序列中所包含元素的數(shù)量、最大和最小元素。
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
二、映射(字典)
映射中的每個元素都有一個名字,如你所知,這個名字專業(yè)的名稱叫鍵。字典(也叫散列表)是Python中唯一內(nèi)建的映射類型。
1、鍵類型
字典的鍵可以是數(shù)字、字符串或者是元組,鍵必須唯一。在Python中,數(shù)字、字符串和元組都被設(shè)計成不可變類型,而常見的列表以及集合(set)都是可變的,所以列表和集合不能作為字典的鍵。鍵可以為任何不可變類型,這正是Python中的字典最強大的地方。
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字典強大之處還包括內(nèi)置了很多常用操作方法,可參考官方文檔,這里不再列舉。
思考:根據(jù)我們使用強類型語言的經(jīng)驗,比如C#和Java,我們肯定會問Python中的字典是線程安全的嗎?
三、集合
集合(Set)在Python 2.3引入,通常使用較新版Python可直接創(chuàng)建,如下所示:
strs=set(['jeff','wong','cnblogs'])
nums=set(range(10))
看上去,集合就是由序列(或者其他可迭代的對象)構(gòu)建的。集合的幾個重要特點和方法如下:
1、副本是被忽略的
集合主要用于檢查成員資格,因此副本是被忽略的,如下示例所示,輸出的集合內(nèi)容是一樣的。
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的字典。
strs=set(['jeff','wong','cnblogs'])
print strs
輸出如下:
set(['wong', 'cnblogs', 'jeff'])
3、集合常用方法
a、交集union
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)運算符“|”可以得到一樣的結(jié)果:
set1=set([1,2,3])
set2=set([2,3,4])
set3=set1|set2
print set1
print set2
print set3
輸出和上面union操作一模一樣的結(jié)果。
其他常見操作包括&(交集),<=,>=,-,copy()等等,這里不再列舉。
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
和序列添加和移除的方法非常類似,可參考官方文檔:
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
集合是可變的,所以不能用做字典的鍵。集合本身只能包含不可變值,所以也就不能包含其他集合:
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類型用于代表不可變(可散列)的集合:
set1=set([1])
set2=set([2])
set1.add(frozenset(set2))
print set1
輸出:
set([1, frozenset([2])])
數(shù)據(jù)分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
LSTM 模型輸入長度選擇技巧:提升序列建模效能的關(guān)鍵? 在循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)家族中,長短期記憶網(wǎng)絡(luò)(LSTM)憑借其解決長序列 ...
2025-07-11CDA 數(shù)據(jù)分析師報考條件詳解與準(zhǔn)備指南? ? 在數(shù)據(jù)驅(qū)動決策的時代浪潮下,CDA 數(shù)據(jù)分析師認證愈發(fā)受到矚目,成為眾多有志投身數(shù) ...
2025-07-11數(shù)據(jù)透視表中兩列相乘合計的實用指南? 在數(shù)據(jù)分析的日常工作中,數(shù)據(jù)透視表憑借其強大的數(shù)據(jù)匯總和分析功能,成為了 Excel 用戶 ...
2025-07-11尊敬的考生: 您好! 我們誠摯通知您,CDA Level I和 Level II考試大綱將于 2025年7月25日 實施重大更新。 此次更新旨在確保認 ...
2025-07-10BI 大數(shù)據(jù)分析師:連接數(shù)據(jù)與業(yè)務(wù)的價值轉(zhuǎn)化者? ? 在大數(shù)據(jù)與商業(yè)智能(Business Intelligence,簡稱 BI)深度融合的時代,BI ...
2025-07-10SQL 在預(yù)測分析中的應(yīng)用:從數(shù)據(jù)查詢到趨勢預(yù)判? ? 在數(shù)據(jù)驅(qū)動決策的時代,預(yù)測分析作為挖掘數(shù)據(jù)潛在價值的核心手段,正被廣泛 ...
2025-07-10數(shù)據(jù)查詢結(jié)束后:分析師的收尾工作與價值深化? ? 在數(shù)據(jù)分析的全流程中,“query end”(查詢結(jié)束)并非工作的終點,而是將數(shù) ...
2025-07-10CDA 數(shù)據(jù)分析師考試:從報考到取證的全攻略? 在數(shù)字經(jīng)濟蓬勃發(fā)展的今天,數(shù)據(jù)分析師已成為各行業(yè)爭搶的核心人才,而 CDA(Certi ...
2025-07-09【CDA干貨】單樣本趨勢性檢驗:捕捉數(shù)據(jù)背后的時間軌跡? 在數(shù)據(jù)分析的版圖中,單樣本趨勢性檢驗如同一位耐心的偵探,專注于從單 ...
2025-07-09year_month數(shù)據(jù)類型:時間維度的精準(zhǔn)切片? ? 在數(shù)據(jù)的世界里,時間是最不可或缺的維度之一,而year_month數(shù)據(jù)類型就像一把精準(zhǔn) ...
2025-07-09CDA 備考干貨:Python 在數(shù)據(jù)分析中的核心應(yīng)用與實戰(zhàn)技巧? ? 在 CDA 數(shù)據(jù)分析師認證考試中,Python 作為數(shù)據(jù)處理與分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 檢驗:數(shù)據(jù)趨勢與突變分析的有力工具? ? ? 在數(shù)據(jù)分析的廣袤領(lǐng)域中,準(zhǔn)確捕捉數(shù)據(jù)的趨勢變化以及識別 ...
2025-07-08備戰(zhàn) CDA 數(shù)據(jù)分析師考試:需要多久?如何規(guī)劃? CDA(Certified Data Analyst)數(shù)據(jù)分析師認證作為國內(nèi)權(quán)威的數(shù)據(jù)分析能力認證 ...
2025-07-08LSTM 輸出不確定的成因、影響與應(yīng)對策略? 長短期記憶網(wǎng)絡(luò)(LSTM)作為循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的一種變體,憑借獨特的門控機制,在 ...
2025-07-07統(tǒng)計學(xué)方法在市場調(diào)研數(shù)據(jù)中的深度應(yīng)用? 市場調(diào)研是企業(yè)洞察市場動態(tài)、了解消費者需求的重要途徑,而統(tǒng)計學(xué)方法則是市場調(diào)研數(shù) ...
2025-07-07CDA數(shù)據(jù)分析師證書考試全攻略? 在數(shù)字化浪潮席卷全球的當(dāng)下,數(shù)據(jù)已成為企業(yè)決策、行業(yè)發(fā)展的核心驅(qū)動力,數(shù)據(jù)分析師也因此成為 ...
2025-07-07剖析 CDA 數(shù)據(jù)分析師考試題型:解鎖高效備考與答題策略? CDA(Certified Data Analyst)數(shù)據(jù)分析師考試作為衡量數(shù)據(jù)專業(yè)能力的 ...
2025-07-04SQL Server 字符串截取轉(zhuǎn)日期:解鎖數(shù)據(jù)處理的關(guān)鍵技能? 在數(shù)據(jù)處理與分析工作中,數(shù)據(jù)格式的規(guī)范性是保證后續(xù)分析準(zhǔn)確性的基礎(chǔ) ...
2025-07-04CDA 數(shù)據(jù)分析師視角:從數(shù)據(jù)迷霧中探尋商業(yè)真相? 在數(shù)字化浪潮席卷全球的今天,數(shù)據(jù)已成為企業(yè)決策的核心驅(qū)動力,CDA(Certifie ...
2025-07-04CDA 數(shù)據(jù)分析師:開啟數(shù)據(jù)職業(yè)發(fā)展新征程? ? 在數(shù)據(jù)成為核心生產(chǎn)要素的今天,數(shù)據(jù)分析師的職業(yè)價值愈發(fā)凸顯。CDA(Certified D ...
2025-07-03