天天看點

python複合資料類型包括_Python中複合資料類型(list,turple以及切片,循環等操作)...

本教程參考網上的一些資料,如有侵權,請聯系删除

清單list

list是一種有序的集合,可以随時添加和删除其中的元素。

清單的表示形式如下c = ['A', 'B', 'C']

print c #輸出['A', 'B', 'C']可用len()函數可以獲得list元素的個數:len(c)

可用索引來通路list中每一個位置的元素:c[0],c[1]

如果要取最後一個元素,還可以用-1做索引,直接擷取倒數第一個元素:c[-1] ,倒數第二個類推c[-2]

可以往list中追加元素到末尾c.append('D')

print c #輸出['A', 'B', 'C','D']也可以把元素插入到指定的位置c.insert(1, 'E')

print c #輸出['A', 'E', 'B', 'C', 'D']也可以删除list末尾的元素c.pop()

print c #輸出['A', 'E', 'B', 'C']也可以把元素替換成别的元素:直接指派給對應的索引位置c[1]='F'

print c #輸出['A', 'F', 'B', 'C']list裡面的元素的資料類型也可以不同,如c=['A',1,True]

元組tuple

tuple稱為元組,和list非常類似,但是tuple一旦初始化就不能修改,比如c=('A','B','C')

現在,c這個tuple不能變了,它沒有append(),insert()這樣的方法。但你可以使用c[0],c[-1],但不能指派成另外的元素。

因為tuple不可變,是以代碼更安全。如果可能,能用tuple代替list就盡量用tuple。

如果要定義一個空的tuple,可以寫成():>>> a = ()

>>> a

()

但是,要定義一個隻有1個元素的tuple,如果你這麼定義:a = (5),這樣隻是定義了5,而不是tuple,正确定義應該為a=(5,)

字典Dictionaries

字典用來儲存(鍵, 值)對。你可以這樣使用它:d = {'cat': 'cute', 'dog': 'furry'} # 建立字典

print d['cat'] # 通過key(cat)通路; prints "cute"

print 'cat' in d # 判斷字典中是否有key; prints "True"

d['fish'] = 'wet' # 向字典加入對

print d['fish'] # Prints "wet"

# print d['monkey'] # KeyError: 'monkey' not a key of d

print d.get('monkey', 'N/A') # Get an element with a default; prints "N/A"

print d.get('fish', 'N/A') # Get an element with a default; prints "wet"

del d['fish'] # 從字典中移除對

print d.get('fish', 'N/A') # "fish" 不再是key了; prints "N/A"

#循環Loops:在字典中,用鍵來疊代更加容易。

d = {'person': 2, 'cat': 4, 'spider': 8}

for animal in d:

legs = d[animal]

print 'A %s has %d legs' % (animal, legs)

# Prints "A person has 2 legs", "A spider has 8 legs", "A cat has 4 legs"

#如果你想要通路鍵和對應的值,那就使用iteritems方法:

d = {'person': 2, 'cat': 4, 'spider': 8}

for animal, legs in d.iteritems():

print 'A %s has %d legs' % (animal, legs)

# Prints "A person has 2 legs", "A spider has 8 legs", "A cat has 4 legs"

#字典推導Dictionary comprehensions:和清單推導類似,但是允許你友善地建構字典。

nums = [0, 1, 2, 3, 4]

even_num_to_square = {x: x ** 2 for x in nums if x % 2 == 0}

print even_num_to_square # Prints "{0: 0, 2: 4, 4: 16}"

切片

python中切片是一個非常好用的功能,下面從代碼說明它的用處c=['ABCD','ABDE']

print c # 輸出['ABC', 'ABD']

print c[0][2:] #輸出CD

nums = range(5) #nums = [0, 1, 2, 3, 4]

print nums[2:4] # prints "[2, 3]"

print nums[2:] # prints "[2, 3, 4]"

print nums[:2] # prints "[0, 1]"

print nums[:] # prints "[0, 1, 2, 3, 4]"

print nums[:-1] # prints "[0, 1, 2, 3]"

nums[2:4] = [8, 9]

print nums # Prints "[0, 1, 8, 9, 4]"

循環Loopsanimals = ['cat', 'dog', 'monkey']

for animal in animals:

print animal

# Prints "cat", "dog", "monkey", each on its own line.

#如果想要在循環體内通路每個元素的指針,可以使用内置的enumerate函數

animals = ['cat', 'dog', 'monkey']

for idx, animal in enumerate(animals):

print '#%d: %s' % (idx + 1, animal)

# Prints "#1: cat", "#2: dog", "#3: monkey", each on its own line

清單推導List comprehensionsnums = [0, 1, 2, 3, 4]

squares = [x ** 2 for x in nums]

print squares # Prints [0, 1, 4, 9, 16]

#清單推導還可以包含條件:

nums = [0, 1, 2, 3, 4]

even_squares = [x ** 2 for x in nums if x % 2 == 0]

print even_squares # Prints "[0, 4, 16]"