四句話總結(jié)
- 列表是一個(gè)有序且可更改的集合,允許重復(fù)成員。
- 元組是一個(gè)有序且不可更改的集合,允許重復(fù)成員。
- 集合是一個(gè)無(wú)序、不可更改*且未索引的集合,沒(méi)有重復(fù)成員。
- 字典是一個(gè)有序且可更改的集合,沒(méi)有重復(fù)成員。
公有的部分
獲取長(zhǎng)度,使用len()
要確定列表中有多少項(xiàng),請(qǐng)使用len()函數(shù)
thislist = [“apple”, “banana”, “cherry”]print(len(thislist))
要確定一個(gè)元組有多少項(xiàng),請(qǐng)使用len()函數(shù)
thistuple = (“apple”, “banana”, “cherry”)print(len(thistuple))
要確定一個(gè)集合有多少項(xiàng),請(qǐng)使用len()函數(shù)。
thisset = {“apple”, “banana”, “cherry”}print(len(thisset))
要確定字典有多少項(xiàng),請(qǐng)使用len()函數(shù)
thisdict = { “brand”: “Ford”, “model”: “Mustang”, “year”: 1964, “year”: 2020}print(len(thisdict))
通過(guò)引用索引號(hào)來(lái)訪問(wèn)
列表項(xiàng)已編制索引,您可以通過(guò)引用索引號(hào)來(lái)訪問(wèn)它們
thislist = [“apple”, “banana”, “cherry”]print(thislist[1])
您可以通過(guò)引用方括號(hào)內(nèi)的索引號(hào)來(lái)訪問(wèn)元組項(xiàng)
thistuple = (“apple”, “banana”, “cherry”)print(thistuple[1])
是否存在指定項(xiàng),請(qǐng)使用in關(guān)鍵字
要確定列表中是否存在指定項(xiàng),請(qǐng)使用in關(guān)鍵字
thislist = [“apple”, “banana”, “cherry”]if “apple” in thislist: print(“Yes, ‘apple’ is in the fruits list”)
要確定元組中是否存在指定項(xiàng),請(qǐng)使用in關(guān)鍵字
thistuple = (“apple”, “banana”, “cherry”)if “apple” in thistuple: print(“Yes, ‘apple’ is in the fruits tuple”)
檢查集合中是否有“香蕉”
thisset = {“apple”, “banana”, “cherry”}print(“banana” in thisset)
要確定字典中是否存在指定的鍵,請(qǐng)使用in關(guān)鍵字
thisdict = { “brand”: “Ford”, “model”: “Mustang”, “year”: 1964}if “model” in thisdict: print(“Yes, ‘model’ is one of the keys in the thisdict dictionary”)
可以使用for循環(huán)遍歷
可以使用for循環(huán)遍歷列表項(xiàng)
thislist = [“apple”, “banana”, “cherry”]for x in thislist: print(x)
可以使用for循環(huán)遍歷元組項(xiàng)
thistuple = (“apple”, “banana”, “cherry”)for x in thistuple: print(x)
在集合中循環(huán),并打印值
thisset = {“apple”, “banana”, “cherry”}for x in thisset: print(x)
循環(huán)字典
for x in thisdict: print(thisdict[x])
還可以使用values()方法返回字典的值
for x in thisdict.values(): print(x)
可以使用keys()方法返回字典的鍵
for x in thisdict.keys(): print(x)
使用items()方法循環(huán)遍歷鍵和值
for x, y in thisdict.items(): print(x, y)
clear()方法清空
clear()方法清空列表。
thislist = [“apple”, “banana”, “cherry”]thislist.clear()print(thislist)
clear()方法清空集合
thisset = {“apple”, “banana”, “cherry”}thisset.clear()print(thisset)
clear()方法清空字典
thisdict = { “brand”: “Ford”, “model”: “Mustang”, “year”: 1964}thisdict.clear()print(thisdict)
del關(guān)鍵字
del關(guān)鍵字還會(huì)刪除指定的索引
thislist = [“apple”, “banana”, “cherry”]del thislist[0]print(thislist)
del關(guān)鍵字也可以完全刪除列表。
thislist = [“apple”, “banana”, “cherry”]del thislist
del關(guān)鍵字將完全刪除集合
thisset = {“apple”, “banana”, “cherry”}del thissetprint(thisset)
del關(guān)鍵字刪除字典具有指定鍵名的項(xiàng)
thisdict = { “brand”: “Ford”, “model”: “Mustang”, “year”: 1964}del thisdict[“model”]print(thisdict)
remove()方法
remove()方法刪除指定的項(xiàng)。
thislist = [“apple”, “banana”, “cherry”]thislist.remove(“banana”)print(thislist)
要?jiǎng)h除集合中的項(xiàng),請(qǐng)使用remove()或discard()方法。
thisset = {“apple”, “banana”, “cherry”}thisset.remove(“banana”)print(thisset)
pop()方法
pop()方法刪除列表指定的索引。
thislist = [“apple”, “banana”, “cherry”]thislist.pop(1)print(thislist)
您也可以使用pop()方法刪除一個(gè)項(xiàng)目,但此方法將刪除最后一個(gè)項(xiàng)目。請(qǐng)記住,集合是無(wú)序的,因此您將不知道刪除了哪些項(xiàng)。
thisset = {“apple”, “banana”, “cherry”}x = thisset.pop()print(x)print(thisset)
pop()方法移除字典具有指定鍵名的項(xiàng)
thisdict = { “brand”: “Ford”, “model”: “Mustang”, “year”: 1964}thisdict.pop(“model”)print(thisdict)
列表
insert()方法在指定的索引處插入項(xiàng)
thislist = [“apple”, “banana”, “cherry”]thislist.insert(2, “watermelon”)print(thislist)
要將項(xiàng)目添加到列表的末尾,請(qǐng)使用append()方法
thislist = [“apple”, “banana”, “cherry”]thislist.append(“orange”)print(thislist)
要將其他列表中的元素附加到當(dāng)前列表,請(qǐng)使用extend()方法。
thislist = [“apple”, “banana”, “cherry”]tropical = [“mango”, “pineapple”, “papaya”]thislist.extend(tropical)print(thislist)
extend()方法不必附加列表,您可以添加任何可迭代對(duì)象(元組、集合、字典等)。
thislist = [“apple”, “banana”, “cherry”]thistuple = (“kiwi”, “orange”)thislist.extend(thistuple)print(thislist)
如果不指定索引,則pop()方法將刪除最后一項(xiàng)。
thislist = [“apple”, “banana”, “cherry”]thislist.pop()print(thislist)
列表理解提供了循環(huán)列表的最短語(yǔ)法:newlist = [*expression* for *item* in *iterable* if *condition* == True]
thislist = [“apple”, “banana”, “cherry”][print(x) for x in thislist]fruits = [“apple”, “banana”, “cherry”, “kiwi”, “mango”]newlist = [x for x in fruits if “a” in x]print(newlist)newlist = [x.upper() for x in fruits]
列表對(duì)象有一個(gè)sort()方法,默認(rèn)情況下,該方法將按字母數(shù)字升序?qū)α斜磉M(jìn)行排序
thislist = [“orange”, “mango”, “kiwi”, “pineapple”, “banana”]thislist.sort()print(thislist)
在排序列表時(shí),我們可以使用內(nèi)置函數(shù)作為關(guān)鍵函數(shù)
thislist = [“banana”, “Orange”, “Kiwi”, “cherry”]thislist.sort(key = str.lower)print(thislist)
reverse()方法反轉(zhuǎn)元素的當(dāng)前排序順序。
thislist = [“banana”, “Orange”, “Kiwi”, “cherry”]thislist.reverse()print(thislist)
有多種方法可以復(fù)制,一種方法是使用內(nèi)置的列表方法copy()。
您不能簡(jiǎn)單地通過(guò)鍵入list2=list1復(fù)制列表,因?yàn)椋簂ist2將僅是對(duì)list1的引用,并且在list1中所做的更改也將自動(dòng)在list2中進(jìn)行。
thislist = [“apple”, “banana”, “cherry”]mylist = thislist.copy()print(mylist)
制作副本的另一種方法是使用內(nèi)置方法list()。
thislist = [“apple”, “banana”, “cherry”]mylist = list(thislist)print(mylist)
在Python中,有幾種方法可以連接或串聯(lián)兩個(gè)或多個(gè)列表。最簡(jiǎn)單的方法之一是使用+運(yùn)算符。
list1 = [“a”, “b”, “c”]list2 = [1, 2, 3]list3 = list1 + list2print(list3)
也可以使用extend()方法,其目的是將元素從一個(gè)列表添加到另一個(gè)列表
list1 = [“a”, “b” , “c”]list2 = [1, 2, 3]list1.extend(list2)print(list1)
元組
要?jiǎng)?chuàng)建只有一個(gè)項(xiàng)的元組,必須在該項(xiàng)后添加逗號(hào),否則Python將無(wú)法將其識(shí)別為元組。
thistuple = (“apple”,)print(type(thistuple))#NOT a tuplethistuple = (“apple”)print(type(thistuple))
您可以將元組轉(zhuǎn)換為列表,更改列表,然后將列表轉(zhuǎn)換回元組。
x = (“apple”, “banana”, “cherry”)y = list(x)y[1] = “kiwi”x = tuple(y)print(x)
將元組添加到元組。您可以將元組添加到元組中,因此如果要添加一個(gè)(或多個(gè))項(xiàng),請(qǐng)使用該項(xiàng)創(chuàng)建一個(gè)新元組,并將其添加到現(xiàn)有元組中.
thistuple = (“apple”, “banana”, “cherry”)y = (“orange”,)thistuple += yprint(thistuple)
我們可以將值提取回變量中,這稱為“拆包”
fruits = (“apple”, “banana”, “cherry”)(green, yellow, red) = fruitsprint(green)print(yellow)print(red)
如果變量的數(shù)量小于值的數(shù)量,則可以在變量名中添加*號(hào),這些值將作為列表分配給變量
fruits = (“apple”, “banana”, “cherry”, “strawberry”, “raspberry”)(green, yellow, *red) = fruitsprint(green)print(yellow)print(red)
要連接兩個(gè)或多個(gè)元組,可以使用+運(yùn)算符
tuple1 = (“a”, “b” , “c”)tuple2 = (1, 2, 3)tuple3 = tuple1 + tuple2print(tuple3)
如果要將元組的內(nèi)容乘以給定的次數(shù),可以使用*運(yùn)算符
fruits = (“apple”, “banana”, “cherry”)mytuple = fruits * 2print(mytuple)
集合
創(chuàng)建集后,不能更改其項(xiàng),但可以添加新項(xiàng)。
thisset = {“apple”, “banana”, “cherry”}thisset.add(“orange”)print(thisset)
要將其他集合中的項(xiàng)添加到當(dāng)前集合中,請(qǐng)使用update()方法。
thisset = {“apple”, “banana”, “cherry”}tropical = {“pineapple”, “mango”, “papaya”}thisset.update(tropical)print(thisset)
可以使用union()方法返回包含兩個(gè)集合中所有項(xiàng)的新集合,也可以使用update()方法將一個(gè)集合中的所有項(xiàng)插入另一個(gè)集合
set1 = {“a”, “b” , “c”}set2 = {1, 2, 3}set3 = set1.union(set2)print(set3)set1 = {“a”, “b” , “c”}set2 = {1, 2, 3}set1.update(set2)print(set1)
intersection_update()方法將只保留兩個(gè)集合中存在的項(xiàng)。
x = {“apple”, “banana”, “cherry”}y = {“google”, “microsoft”, “apple”}x.intersection_update(y)print(x)
intersection()方法將返回一個(gè)新的集合,該集合只包含兩個(gè)集合中存在的項(xiàng)。
x = {“apple”, “banana”, “cherry”}y = {“google”, “microsoft”, “apple”}z = x.intersection(y)print(z)
symmetric_difference_update()方法將只保留兩個(gè)集合中不存在的元素。
x = {“apple”, “banana”, “cherry”}y = {“google”, “microsoft”, “apple”}x.symmetric_difference_update(y)print(x)
symmetric_difference()方法將返回一個(gè)新的集合,該集合只包含兩個(gè)集合中不存在的元素。
x = {“apple”, “banana”, “cherry”}y = {“google”, “microsoft”, “apple”}z = x.symmetric_difference(y)print(z)
字典
您可以通過(guò)在方括號(hào)內(nèi)引用字典的鍵名來(lái)訪問(wèn)字典的項(xiàng)
thisdict = { “brand”: “Ford”, “model”: “Mustang”, “year”: 1964}x = thisdict[“model”]
還有一個(gè)名為get()的方法,它將給出相同的結(jié)果
x = thisdict.get(“model”)
keys()方法將返回字典中所有鍵的列表。
x = thisdict.keys()
values()方法將返回字典中所有值的列表。
x = thisdict.values()
items()方法將返回字典中的每個(gè)項(xiàng),作為列表中的元組。
x = thisdict.items()
返回的列表是字典項(xiàng)的視圖,這意味著對(duì)字典所做的任何更改都將反映在項(xiàng)列表中。
car = {“brand”: “Ford”,”model”: “Mustang”,”year”: 1964}x = car.items()print(x) #before the changecar[“year”] = 2020print(x) #after the change
popitem()方法刪除最后插入的項(xiàng)(在3.7之前的版本中,將刪除隨機(jī)項(xiàng))
thisdict = { “brand”: “Ford”, “model”: “Mustang”, “year”: 1964}thisdict.popitem()print(thisdict)
您不能簡(jiǎn)單地通過(guò)鍵入dict2=dict1來(lái)復(fù)制字典,因?yàn)椋篸ict2將僅是對(duì)dict1的引用,在dict1中所做的更改也將自動(dòng)在dict2中進(jìn)行。
thisdict = { “brand”: “Ford”, “model”: “Mustang”, “year”: 1964}mydict = thisdict.copy()print(mydict)
字典可以包含字典,這稱為嵌套字典。
myfamily = { “child1” : { “name” : “Emil”, “year” : 2004 }, “child2” : { “name” : “Tobias”, “year” : 2007 }, “child3” : { “name” : “Linus”, “year” : 2011 }}child1 = { “name” : “Emil”, “year” : 2004}child2 = { “name” : “Tobias”, “year” : 2007}child3 = { “name” : “Linus”, “year” : 2011}myfamily = { “child1” : child1, “child2” : child2, “child3” : child3}