在线不卡日本ⅴ一区v二区_精品一区二区中文字幕_天堂v在线视频_亚洲五月天婷婷中文网站

  • <menu id="lky3g"></menu>
  • <style id="lky3g"></style>
    <pre id="lky3g"><tt id="lky3g"></tt></pre>

    Python入門系列(四)傻傻分不清:列表、元組、字典、集合的區(qū)別

    四句話總結(jié)

    • 列表是一個有序且可更改的集合,允許重復(fù)成員
    • 元組是一個有序且不可更改的集合,允許重復(fù)成員。
    • 集合是一個無序、不可更改*且未索引的集合,沒有重復(fù)成員。
    • 字典是一個有序且可更改的集合,沒有重復(fù)成員。

    公有的部分

    獲取長度,使用len()

    要確定列表中有多少項,請使用len()函數(shù)

    thislist = [“apple”, “banana”, “cherry”]print(len(thislist))

    要確定一個元組有多少項,請使用len()函數(shù)

    thistuple = (“apple”, “banana”, “cherry”)print(len(thistuple))

    要確定一個集合有多少項,請使用len()函數(shù)。

    thisset = {“apple”, “banana”, “cherry”}print(len(thisset))

    要確定字典有多少項,請使用len()函數(shù)

    thisdict = { “brand”: “Ford”, “model”: “Mustang”, “year”: 1964, “year”: 2020}print(len(thisdict))

    通過引用索引號來訪問

    列表項已編制索引,您可以通過引用索引號來訪問它們

    thislist = [“apple”, “banana”, “cherry”]print(thislist[1])

    您可以通過引用方括號內(nèi)的索引號來訪問元組項

    thistuple = (“apple”, “banana”, “cherry”)print(thistuple[1])

    是否存在指定項,請使用in關(guān)鍵字

    要確定列表中是否存在指定項,請使用in關(guān)鍵字

    thislist = [“apple”, “banana”, “cherry”]if “apple” in thislist: print(“Yes, ‘apple’ is in the fruits list”)

    要確定元組中是否存在指定項,請使用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)

    要確定字典中是否存在指定的鍵,請使用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)遍歷列表項

    thislist = [“apple”, “banana”, “cherry”]for x in thislist: print(x)

    可以使用for循環(huán)遍歷元組項

    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)鍵字還會刪除指定的索引

    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)鍵字刪除字典具有指定鍵名的項

    thisdict = { “brand”: “Ford”, “model”: “Mustang”, “year”: 1964}del thisdict[“model”]print(thisdict)

    remove()方法

    remove()方法刪除指定的項。

    thislist = [“apple”, “banana”, “cherry”]thislist.remove(“banana”)print(thislist)

    要刪除集合中的項,請使用remove()或discard()方法。

    thisset = {“apple”, “banana”, “cherry”}thisset.remove(“banana”)print(thisset)

    pop()方法

    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)

    要將其他列表中的元素附加到當(dāng)前列表,請使用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)

    列表理解提供了循環(huán)列表的最短語法: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()方法,默認(rèn)情況下,該方法將按字母數(shù)字升序?qū)α斜磉M(jìn)行排序

    thislist = [“orange”, “mango”, “kiwi”, “pineapple”, “banana”]thislist.sort()print(thislist)

    在排序列表時,我們可以使用內(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()。

    您不能簡單地通過鍵入list2=list1復(fù)制列表,因?yàn)椋簂ist2將僅是對list1的引用,并且在list1中所做的更改也將自動在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)兩個或多個列表。最簡單的方法之一是使用+運(yùn)算符。

    list1 = [“a”, “b”, “c”]list2 = [1, 2, 3]list3 = list1 + list2print(list3)

    也可以使用extend()方法,其目的是將元素從一個列表添加到另一個列表

    list1 = [“a”, “b” , “c”]list2 = [1, 2, 3]list1.extend(list2)print(list1)

    元組

    要創(chuàng)建只有一個項的元組,必須在該項后添加逗號,否則Python將無法將其識別為元組。

    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)

    將元組添加到元組。您可以將元組添加到元組中,因此如果要添加一個(或多個)項,請使用該項創(chuàng)建一個新元組,并將其添加到現(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ù)量,則可以在變量名中添加*號,這些值將作為列表分配給變量

    fruits = (“apple”, “banana”, “cherry”, “strawberry”, “raspberry”)(green, yellow, *red) = fruitsprint(green)print(yellow)print(red)

    要連接兩個或多個元組,可以使用+運(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)建集后,不能更改其項,但可以添加新項。

    thisset = {“apple”, “banana”, “cherry”}thisset.add(“orange”)print(thisset)

    要將其他集合中的項添加到當(dāng)前集合中,請使用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)

    字典

    您可以通過在方括號內(nèi)引用字典的鍵名來訪問字典的項

    thisdict = { “brand”: “Ford”, “model”: “Mustang”, “year”: 1964}x = thisdict[“model”]

    還有一個名為get()的方法,它將給出相同的結(jié)果

    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 changecar[“year”] = 2020print(x) #after the change

    popitem()方法刪除最后插入的項(在3.7之前的版本中,將刪除隨機(jī)項)

    thisdict = { “brand”: “Ford”, “model”: “Mustang”, “year”: 1964}thisdict.popitem()print(thisdict)

    您不能簡單地通過鍵入dict2=dict1來復(fù)制字典,因?yàn)椋篸ict2將僅是對dict1的引用,在dict1中所做的更改也將自動在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}

    鄭重聲明:本文內(nèi)容及圖片均整理自互聯(lián)網(wǎng),不代表本站立場,版權(quán)歸原作者所有,如有侵權(quán)請聯(lián)系管理員(admin#wlmqw.com)刪除。
    上一篇 2022年8月22日 18:11
    下一篇 2022年8月22日 18:12

    相關(guān)推薦

    聯(lián)系我們

    聯(lián)系郵箱:admin#wlmqw.com
    工作時間:周一至周五,10:30-18:30,節(jié)假日休息