總結分析列表、元組、字典、集合的相同與區別之處,只有徹底分清之後,就會在應用的時候,得心應手。
len()
要確定列表中有多少項,請使用len()函數
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
要確定一個元組有多少項,請使用len()函數
thistuple = ("apple", "banana", "cherry")
print(len(thistuple))
要確定一個集合有多少項,請使用len()函數。
thisset = {"apple", "banana", "cherry"}
print(len(thisset))
要確定字典有多少項,請使用len()函數
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
}
print(len(thisdict))
列表項已編制索引,您可以通過參照索引號來存取它們
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
您可以通過參照方括號內的索引號來存取元組項
thistuple = ("apple", "banana", "cherry")
print(thistuple[1])
in
關鍵字要確定列表中是否存在指定項,請使用in
關鍵字
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
要確定元組中是否存在指定項,請使用in
關鍵字
thistuple = ("apple", "banana", "cherry")
if "apple" in thistuple:
print("Yes, 'apple' is in the fruits tuple")
檢查集合中是否有「香蕉」
thisset = {"apple", "banana", "cherry"}
print("banana" in thisset)
要確定字典中是否存在指定的鍵,請使用in關鍵字
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary")
可以使用for迴圈遍歷列表項
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
可以使用for迴圈遍歷元組項
thistuple = ("apple", "banana", "cherry")
for x in thistuple:
print(x)
在集合中迴圈,並列印值
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
迴圈字典
for x in thisdict:
print(thisdict[x])
還可以使用values()方法返回字典的值
for x in thisdict.values():
print(x)
可以使用keys()方法返回字典的鍵
for x in thisdict.keys():
print(x)
使用items()方法迴圈遍歷鍵和值
for x, y in thisdict.items():
print(x, y)
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關鍵字還會刪除指定的索引
thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)
del關鍵字也可以完全刪除列表。
thislist = ["apple", "banana", "cherry"]
del thislist
del關鍵字將完全刪除集合
thisset = {"apple", "banana", "cherry"}
del thisset
print(thisset)
del關鍵字刪除字典具有指定鍵名的項
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict["model"]
print(thisdict)
remove()方法刪除指定的項。
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
要刪除集合中的項,請使用remove()或discard()方法。
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)
pop()方法刪除列表指定的索引。
thislist = ["apple", "banana", "cherry"]
thislist.pop(1)
print(thislist)
您也可以使用pop()方法刪除一個專案,但此方法將刪除最後一個專案。請記住,集合是無序的,因此您將不知道刪除了哪些項
。
thisset = {"apple", "banana", "cherry"}
x = thisset.pop()
print(x)
print(thisset)
pop()方法移除字典具有指定鍵名的項
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.pop("model")
print(thisdict)
insert()方法在指定的索引處插入項
thislist = ["apple", "banana", "cherry"]
thislist.insert(2, "watermelon")
print(thislist)
要將專案新增到列表的末尾,請使用append()方法
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
要將其他列表中的元素附加到當前列表,請使用extend()方法。
thislist = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
thislist.extend(tropical)
print(thislist)
extend()方法不必附加列表,您可以新增任何可迭代物件(元組、集合、字典等)。
thislist = ["apple", "banana", "cherry"]
thistuple = ("kiwi", "orange")
thislist.extend(thistuple)
print(thislist)
如果不指定索引,則pop()方法將刪除最後一項。
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
列表理解提供了迴圈列表的最短語法: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]
列表物件有一個sort()方法,預設情況下,該方法將按字母數位升序對列表進行排序
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)
在排序列表時,我們可以使用內建函數作為關鍵函數
thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.sort(key = str.lower)
print(thislist)
reverse()方法反轉元素的當前排序順序。
thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.reverse()
print(thislist)
有多種方法可以複製,一種方法是使用內建的列表方法copy()。
您不能簡單地通過鍵入list2=list1複製列表,因為:list2僅僅是對list1的參照,並且在list1中所做的更改也將自動在list2中進行。
thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)
製作副本的另一種方法是使用內建方法list()。
thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist)
在Python中,有幾種方法可以連線或串聯兩個或多個列表。最簡單的方法之一是使用+運運算元。
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
也可以使用extend()方法,其目的是將元素從一個列表新增到另一個列表
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)
要建立只有一個項的元組,必須在該項後新增逗號,否則Python將無法將其識別為元組。
thistuple = ("apple",)
print(type(thistuple))
#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
您可以將元組轉換為列表,更改列表,然後將列表轉換回元組。
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
將元組新增到元組。您可以將元組新增到元組中,因此如果要新增一個(或多個)項,請使用該項建立一個新元組,並將其新增到現有元組中.
thistuple = ("apple", "banana", "cherry")
y = ("orange",)
thistuple += y
print(thistuple)
我們可以將值提取回變數中,這稱為「拆包」
fruits = ("apple", "banana", "cherry")
(green, yellow, red) = fruits
print(green)
print(yellow)
print(red)
如果變數的數量小於值的數量,則可以在變數名中新增*號,這些值將作為列表分配給變數
fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")
(green, yellow, *red) = fruits
print(green)
print(yellow)
print(red)
要連線兩個或多個元組,可以使用+運運算元
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
如果要將元組的內容乘以給定的次數,可以使用*運運算元
fruits = ("apple", "banana", "cherry")
mytuple = fruits * 2
print(mytuple)
建立集後,不能更改其項,但可以新增新項。
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
要將其他集合中的項新增到當前集合中,請使用update()方法。
thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
print(thisset)
可以使用union()方法返回包含兩個集合中所有項的新集合,也可以使用update()方法將一個集合中的所有項插入另一個集合
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()方法將只保留兩個集合中存在的項。
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.intersection_update(y)
print(x)
intersection()方法將返回一個新的集合,該集合只包含兩個集合中存在的項。
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.intersection(y)
print(z)
symmetric_difference_update()方法將只保留兩個集合中不存在的元素。
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.symmetric_difference_update(y)
print(x)
symmetric_difference()方法將返回一個新的集合,該集合只包含兩個集合中不存在的元素。
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.symmetric_difference(y)
print(z)
您可以通過在方括號內參照字典的鍵名來存取字典的項
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]
還有一個名為get()的方法,它將給出相同的結果
x = thisdict.get("model")
keys()方法將返回字典中所有鍵的列表。
x = thisdict.keys()
values()方法將返回字典中所有值的列表。
x = thisdict.values()
items()方法將返回字典中的每個項,作為列表中的元組。
x = thisdict.items()
返回的列表是字典項的檢視,這意味著對字典所做的任何更改都將反映在項列表中。
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.items()
print(x) #before the change
car["year"] = 2020
print(x) #after the change
popitem()方法刪除最後插入的項(在3.7之前的版本中,將刪除隨機項)
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.popitem()
print(thisdict)
您不能簡單地通過鍵入dict2=dict1來複制字典,因為:dict2將僅是對dict1的參照,在dict1中所做的更改也將自動在dict2中進行。
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
}
您的關注,是我的無限動力!
公眾號 @生活處處有BUG