一Python 集合
Python 編程語言中有四種集合數(shù)據(jù)類型:
列表(List)是一種有序和可更改的集合。允許重復(fù)的成員。
元組(Tuple)是一種有序且不可更改的集合。允許重復(fù)的成員。
集合(Set)是一個無序和無索引的集合。沒有重復(fù)的成員。
字典(Dictionary)是一個無序,可變和有索引的集合。沒有重復(fù)的成員。
二.列表(List)
列表是一個有序且可更改的集合。在 Python 中,列表用方括號編寫。
實例
1.創(chuàng)建列表:
thislist = ["apple", "banana", "cherry"]
print(thislist)
2.訪問項目
您可以通過引用索引號來訪問列表項:
實例
打印列表的第二項:
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
3.負(fù)的索引
負(fù)索引表示從末尾開始,-1 表示最后一個項目,-2 表示倒數(shù)第二個項目,依此類推。
實例
打印列表的最后一項:
thislist = ["apple", "banana", "cherry"]
print(thislist[-1])
索引范圍
您可以通過指定范圍的起點和終點來指定索引范圍。
指定范圍后,返回值將是包含指定項目的新列表。
實例
返回第三、第四、第五項:
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])
注釋:搜索將從索引 2(包括)開始,到索引 5(不包括)結(jié)束。
請記住,第一項的索引為 0。
負(fù)索引的范圍
如果要從列表末尾開始搜索,請指定負(fù)索引:
實例
此例將返回從索引 -4(包括)到索引 -1(排除)的項目:
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[-4:-1])
4.更改項目
如需更改特定項目的值,請引用索引號:
實例
更改第二項:
thislist = ["apple", "banana", "cherry"]
thislist[1] = "mango"
print(thislist)
5.遍歷列表
您可以使用 for 循環(huán)遍歷列表項:
實例
逐個打印列表中的所有項目:
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
您將在 Python For 循環(huán) 這一章中學(xué)習(xí)有關(guān) for 循環(huán)的更多知識。
6.檢查項目是否存在
如需確定列表中是否存在指定的項,請使用 in 關(guān)鍵字:
實例
檢查列表中是否存在 “apple”:
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
7.列表長度
如需確定列表中有多少項,請使用 len() 方法:
實例
打印列表中的項目數(shù):
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
8.添加項目
如需將項目添加到列表的末尾,請使用 append() 方法:
實例
使用 append() 方法追加項目:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
9.要在指定的索引處添加項目,請使用 insert() 方法:
實例
插入項目作為第二個位置:
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)
10.刪除項目
有幾種方法可以從列表中刪除項目:
實例
remove() 方法刪除指定的項目:
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
實例
pop() 方法刪除指定的索引(如果未指定索引,則刪除最后一項):
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
實例
del 關(guān)鍵字刪除指定的索引:
thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)
實例
del 關(guān)鍵字也能完整地刪除列表:
thislist = ["apple", "banana", "cherry"]
del thislist
實例
clear() 方法清空列表:
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)
11.復(fù)制列表
您只能通過鍵入 list2 = list1 來復(fù)制列表,因為:list2 將只是對 list1 的引用,list1 中所做的更改也將自動在 list2 中進(jìn)行。
有一些方法可以進(jìn)行復(fù)制,一種方法是使用內(nèi)置的 List 方法 copy()。
實例
使用 copy() 方法來復(fù)制列表:
thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)
制作副本的另一種方法是使用內(nèi)建的方法 list()。
實例
使用 list() 方法復(fù)制列表:
thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist)
12.合并兩個列表
在 Python 中,有幾種方法可以連接或串聯(lián)兩個或多個列表。
最簡單的方法之一是使用 + 運算符。
實例
合并兩個列表:
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
連接兩個列表的另一種方法是將 list2 中的所有項一個接一個地追加到 list1 中:
實例
把 list2 追加到 list1 中:
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
for x in list2:
list1.append(x)
print(list1)
或者,您可以使用 extend() 方法,其目的是將一個列表中的元素添加到另一列表中:
實例
使用 extend() 方法將 list2 添加到 list1 的末尾:
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)
13.list() 構(gòu)造函數(shù)
也可以使用 list() 構(gòu)造函數(shù)創(chuàng)建一個新列表。
實例
使用 list() 構(gòu)造函數(shù)創(chuàng)建列表:
thislist = list(("apple", "banana", "cherry")) # 請注意雙括號
print(thislist)
三.元組(Tuple)
元組是有序且不可更改的集合。在 Python 中,元組是用圓括號編寫的。
實例
1.創(chuàng)建元組:
thistuple = ("apple", "banana", "cherry")
print(thistuple)
2.訪問元組項目
您可以通過引用方括號內(nèi)的索引號來訪問元組項目:
實例
打印元組中的第二個項目:
thistuple = ("apple", "banana", "cherry")
print(thistuple[1])
3.負(fù)索引
負(fù)索引表示從末尾開始,-1 表示最后一個項目,-2 表示倒數(shù)第二個項目,依此類推。
實例
打印元組的最后一個項目:
thistuple = ("apple", "banana", "cherry")
print(thistuple[-1])
索引范圍
您可以通過指定范圍的起點和終點來指定索引范圍。
指定范圍后,返回值將是帶有指定項目的新元組。
實例
返回第三、第四、第五個項目:
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[2:5])
注釋:搜索將從索引 2(包括)開始,到索引 5(不包括)結(jié)束。請記住,第一項的索引為 0。
負(fù)索引范圍
如果要從元組的末尾開始搜索,請指定負(fù)索引:
實例
此例將返回從索引 -4(包括)到索引 -1(排除)的項目:
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[-4:-1])
4.更改元組值
創(chuàng)建元組后,您將無法更改其值。元組是不可變的,或者也稱為恒定的。
但是有一種解決方法。您可以將元組轉(zhuǎn)換為列表,更改列表,然后將列表轉(zhuǎn)換回元組。
實例
把元組轉(zhuǎn)換為列表即可進(jìn)行更改:
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
5.遍歷元組
您可以使用 for 循環(huán)遍歷元組項目。
實例
遍歷項目并打印值:
thistuple = ("apple", "banana", "cherry")
for x in thistuple:
print(x)
6.檢查項目是否存在
要確定元組中是否存在指定的項,請使用 in 關(guān)鍵字:
實例
檢查元組中是否存在 “apple”:
thistuple = ("apple", "banana", "cherry")
if "apple" in thistuple:
print("Yes, 'apple' is in the fruits tuple")
7.元組長度
要確定元組有多少項,請使用 len() 方法:
實例
打印元組中的項目數(shù)量:
thistuple = ("apple", "banana", "cherry")
print(len(thistuple))
8.添加項目
元組一旦創(chuàng)建,您就無法向其添加項目。元組是不可改變的。
實例
您無法向元組添加項目:
thistuple = ("apple", "banana", "cherry")
thistuple[3] = "orange" # 會引發(fā)錯誤
print(thistuple)
創(chuàng)建有一個項目的元組
如需創(chuàng)建僅包含一個項目的元組,您必須在該項目后添加一個逗號,否則 Python 無法將變量識別為元組。
實例
單項元組,別忘了逗號:
thistuple = ("apple",)
print(type(thistuple))
#不是元組
thistuple = ("apple")
print(type(thistuple))
9.刪除項目
注釋:您無法刪除元組中的項目。
元組是不可更改的,因此您無法從中刪除項目,但您可以完全刪除元組:
實例
del 關(guān)鍵字可以完全刪除元組:
thistuple = ("apple", "banana", "cherry")
del thistuple
print(thistuple) # 這會引發(fā)錯誤,因為元組已不存在
10.合并兩個元組
如需連接兩個或多個元組,您可以使用 + 運算符:
實例
合并這個元組:
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
11.tuple() 構(gòu)造函數(shù)
也可以使用 tuple() 構(gòu)造函數(shù)來創(chuàng)建元組。
實例
使用 tuple() 方法來創(chuàng)建元組:
thistuple = tuple(("apple", "banana", "cherry")) # 請注意雙括號
print(thistuple)
四.集合(Set)
集合是無序和無索引的集合。在 Python 中,集合用花括號編寫。
實例
1.創(chuàng)建集合:
thisset = {"apple", "banana", "cherry"}
print(thisset)
注釋:集合是無序的,因此您無法確定項目的顯示順序。
2.訪問項目
您無法通過引用索引來訪問 set 中的項目,因為 set 是無序的,項目沒有索引。
但是您可以使用 for 循環(huán)遍歷 set 項目,或者使用 in 關(guān)鍵字查詢集合中是否存在指定值。
實例
遍歷集合,并打印值:
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
實例
檢查 set 中是否存在 “banana”:
thisset = {"apple", "banana", "cherry"}
print("banana" in thisset)
3.更改項目
集合一旦創(chuàng)建,您就無法更改項目,但是您可以添加新項目。
4.添加項目
要將一個項添加到集合,請使用 add() 方法。
要向集合中添加多個項目,請使用 update() 方法。
實例
使用 add() 方法向 set 添加項目:
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
實例
使用 update() 方法將多個項添加到集合中:
thisset = {"apple", "banana", "cherry"}
thisset.update(["orange", "mango", "grapes"])
print(thisset)
5.獲取 Set 的長度
要確定集合中有多少項,請使用 len() 方法。
實例
獲取集合中的項目數(shù):
thisset = {"apple", "banana", "cherry"}
print(len(thisset))
6.刪除項目
要刪除集合中的項目,請使用 remove() 或 discard() 方法。
實例
使用 remove() 方法來刪除 “banana”:
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)
注釋:如果要刪除的項目不存在,則 remove() 將引發(fā)錯誤。
實例
使用 discard() 方法來刪除 “banana”:
thisset = {"apple", "banana", "cherry"}
thisset.discard("banana")
print(thisset)
注釋:如果要刪除的項目不存在,則 discard() 不會引發(fā)錯誤。
您還可以使用 pop() 方法刪除項目,但此方法將刪除最后一項。請記住,set 是無序的,因此您不會知道被刪除的是什么項目。
pop() 方法的返回值是被刪除的項目。
實例
使用 pop() 方法刪除最后一項:
thisset = {"apple", "banana", "cherry"}
x = thisset.pop()
print(x)
print(thisset)
注釋:集合是無序的,因此在使用 pop() 方法時,您不會知道刪除的是哪個項目。
實例
clear() 方法清空集合:
thisset = {"apple", "banana", "cherry"}
thisset.clear()
print(thisset)
實例
del 徹底刪除集合:
thisset = {"apple", "banana", "cherry"}
del thisset
print(thisset)
7.合并兩個集合
在 Python 中,有幾種方法可以連接兩個或多個集合。
您可以使用 union() 方法返回包含兩個集合中所有項目的新集合,也可以使用 update() 方法將一個集合中的所有項目插入另一個集合中:
實例
union() 方法返回一個新集合,其中包含兩個集合中的所有項目:
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)
實例
update() 方法將 set2 中的項目插入 set1 中:
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set1.update(set2)
print(set1)
8.set() 構(gòu)造函數(shù)
也可以使用 set() 構(gòu)造函數(shù)來創(chuàng)建集合。
實例
thisset = set(("apple", "banana", "cherry")) # 請留意這個雙括號
print(thisset)
五.字典(Dictionary)
字典是一個無序、可變和有索引的集合。在 Python 中,字典用花括號編寫,擁有鍵和值。
實例
1.創(chuàng)建并打印字典:
thisdict = {
"brand": "Porsche",
"model": "911",
"year": 1963
}
print(thisdict)
2.訪問項目
您可以通過在方括號內(nèi)引用其鍵名來訪問字典的項目:
實例
獲取 “model” 鍵的值:
x = thisdict["model"]
還有一個名為 get() 的方法會給你相同的結(jié)果:
實例
獲取 “model” 鍵的值:
x = thisdict.get("model")
3.更改值
您可以通過引用其鍵名來更改特定項的值:
實例
把 “year” 改為 2019:
thisdict = {
"brand": "Porsche",
"model": "911",
"year": 1963
}
thisdict["year"] = 2019
4.遍歷字典
您可以使用 for 循環(huán)遍歷字典。
循環(huán)遍歷字典時,返回值是字典的鍵,但也有返回值的方法。
實例
逐個打印字典中的所有鍵名:
for x in thisdict:
print(x)
實例
逐個打印字典中的所有值:
for x in thisdict:
print(thisdict[x])
實例
您還可以使用 values() 函數(shù)返回字典的值:
for x in thisdict.values():
print(x)
實例
通過使用 items() 函數(shù)遍歷鍵和值:
for x, y in thisdict.items():
print(x, y)
5.檢查鍵是否存在
要確定字典中是否存在指定的鍵,請使用 in 關(guān)鍵字:
實例
檢查字典中是否存在 “model”:
thisdict = {
"brand": "Porsche",
"model": "911",
"year": 1963
}
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary")
6.字典長度
要確定字典有多少項目(鍵值對),請使用 len() 方法。
實例
打印字典中的項目數(shù):
print(len(thisdict))
7.添加項目
通過使用新的索引鍵并為其賦值,可以將項目添加到字典中:
實例
thisdict = {
"brand": "Porsche",
"model": "911",
"year": 1963
}
thisdict["color"] = "red"
print(thisdict)
8.刪除項目
有幾種方法可以從字典中刪除項目:
實例
pop() 方法刪除具有指定鍵名的項:
thisdict = {
"brand": "Porsche",
"model": "911",
"year": 1963
}
thisdict.pop("model")
print(thisdict)
實例
popitem() 方法刪除最后插入的項目(在 3.7 之前的版本中,刪除隨機(jī)項目):
thisdict = {
"brand": "Porsche",
"model": "911",
"year": 1963
}
thisdict.popitem()
print(thisdict)
實例
del 關(guān)鍵字刪除具有指定鍵名的項目:
thisdict = {
"brand": "Porsche",
"model": "911",
"year": 1963
}
del thisdict["model"]
print(thisdict)
實例
del 關(guān)鍵字也可以完全刪除字典:
thisdict = {
"brand": "Porsche",
"model": "911",
"year": 1963
}
del thisdict
print(thisdict) #this 會導(dǎo)致錯誤,因為 "thisdict" 不再存在。
實例
clear() 關(guān)鍵字清空字典:
thisdict = {
"brand": "Porsche",
"model": "911",
"year": 1963
}
thisdict.clear()
print(thisdict)
9.復(fù)制字典
您不能通過鍵入 dict2 = dict1 來復(fù)制字典,因為:dict2 只是對 dict1 的引用,而 dict1 中的更改也將自動在 dict2 中進(jìn)行。
有一些方法可以進(jìn)行復(fù)制,一種方法是使用內(nèi)建的字典方法 copy()。
實例
使用 copy() 方法來復(fù)制字典:
thisdict = {
"brand": "Porsche",
"model": "911",
"year": 1963
}
mydict = thisdict.copy()
print(mydict)
制作副本的另一種方法是使用內(nèi)建方法 dict()。文章來源:http://www.zghlxwxcb.cn/news/detail-780941.html
實例
使用 dict() 方法創(chuàng)建字典的副本:文章來源地址http://www.zghlxwxcb.cn/news/detail-780941.html
thisdict = {
"brand": "Porsche",
"model": "911",
"year": 1963
}
mydict = dict(thisdict)
print(mydict)
到了這里,關(guān)于Python 集合(列表 ,元組,集合, 字典)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!