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

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

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

    四句話總結(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}

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

    相關(guān)推薦

    • 分享4條發(fā)微商朋友圈的方法(微商朋友圈應(yīng)該怎么發(fā))

      對(duì)于微商朋友來(lái)說(shuō),朋友圈的重要性不言而喻了。 那么微商的朋友圈到底該怎么發(fā)呢? 為什么同樣是經(jīng)營(yíng)一個(gè)朋友圈,有的微商看起來(lái)逼格滿滿,實(shí)際效果也不錯(cuò);而有的卻動(dòng)都不動(dòng)就被屏蔽甚至拉黑…

      2022年11月27日
    • 存儲(chǔ)過(guò)程語(yǔ)法(sql server存儲(chǔ)過(guò)程語(yǔ)法)

      今天小編給各位分享存儲(chǔ)過(guò)程語(yǔ)法的知識(shí),其中也會(huì)對(duì)sql server存儲(chǔ)過(guò)程語(yǔ)法進(jìn)行解釋,如果能碰巧解決你現(xiàn)在面臨的問(wèn)題,別忘了關(guān)注本站,現(xiàn)在開(kāi)始吧! oracle存儲(chǔ)過(guò)程基本語(yǔ)法…

      2022年11月26日
    • 抖音帶貨怎么做入門(抖音帶貨怎么做入門教學(xué))

      相信很多小伙伴都有注意到,現(xiàn)在抖音已經(jīng)成為大家最常光顧的一個(gè)平臺(tái)了,作為一個(gè)日活破億的流量池,如今抖音上的用戶數(shù)量極大。因此,現(xiàn)在在抖音上帶貨、賣貨的人也是越來(lái)越多了,那么想在抖音…

      2022年11月25日
    • 《寶可夢(mèng)朱紫》夢(mèng)特性怎么獲得?隱藏特性獲取方法推薦

      寶可夢(mèng)朱紫里有很多寶可夢(mèng)都是擁有夢(mèng)特性會(huì)變強(qiáng)的寶可夢(mèng),很多玩家不知道夢(mèng)特性怎么獲得,下面就給大家?guī)?lái)寶可夢(mèng)朱紫隱藏特性獲取方法推薦,感興趣的小伙伴一起來(lái)看看吧,希望能幫助到大家。 …

      2022年11月25日
    • 《寶可夢(mèng)朱紫》奇魯莉安怎么進(jìn)化?奇魯莉安進(jìn)化方法分享

      寶可夢(mèng)朱紫中的奇魯莉安要怎么進(jìn)化呢?很多玩家都不知道,下面就給大家?guī)?lái)寶可夢(mèng)朱紫奇魯莉安進(jìn)化方法分享,感興趣的小伙伴一起來(lái)看看吧,希望能幫助到大家。 奇魯莉安進(jìn)化方法分享 奇魯莉安…

      2022年11月25日
    • 鬧劇落下帷幕,曼聯(lián)官宣 C 羅離隊(duì)

      1、鬧劇落下帷幕,曼聯(lián)官宣 C 羅離隊(duì) 在經(jīng)歷過(guò)半個(gè)賽季的激烈鬧劇、C 羅私自接受采訪炮轟曼聯(lián)之后,俱樂(lè)部終于做出了相對(duì)應(yīng)的措施:正式官宣 C 羅離隊(duì)。 在曼聯(lián)俱樂(lè)部發(fā)布的聲明中寫…

      2022年11月24日
    • 《原神》3.2無(wú)相交響詩(shī)第一天無(wú)相之冰怎么打?無(wú)相交響詩(shī)攻略

      原神3.2無(wú)相交響詩(shī)第一天無(wú)相之冰怎么打?最近新版本3.2版本的無(wú)相交響詩(shī)活動(dòng)又開(kāi)啟了,不少玩家還不清楚具體的玩法,下面一起來(lái)看一下原神被隱去的原神3.2無(wú)相交響詩(shī)第一天無(wú)相之冰打…

      2022年11月24日
    • iqoo11什么時(shí)候上市 iqoo11發(fā)布時(shí)間最新消息

      iqoo11什么時(shí)候發(fā)布?隨著新一代旗艦芯片的發(fā)布,各家手機(jī)廠商也是公布了自己的旗艦機(jī),那么iqoo11什么時(shí)候發(fā)布呢?下面就讓小編為大家介紹一下,一起來(lái)看看吧。 iqoo11什么…

      2022年11月24日
    • 曝小米13系列已量產(chǎn):起步價(jià)格或定在4500元左右

      高通目前已經(jīng)發(fā)布第二代驍龍8芯片,首批機(jī)型已經(jīng)蓄勢(shì)待發(fā),小米此前也已經(jīng)宣布新旗艦要率先搭載。 據(jù)澎湃報(bào)道,小米13系列已經(jīng)正式量產(chǎn),全系均搭載4nm芯片,不出意外是標(biāo)配第二代驍龍8…

      2022年11月24日
    • 王者榮耀高幀率模式支持機(jī)型列表最新 新增機(jī)型名單

      11月24日上午8:30-9:30,《王者榮耀》將進(jìn)行全服不停機(jī)更新。本次更新大小安卓約33MB,iOS約44MB。 為了讓玩家們能享受更平滑,更精美的王者榮耀操作畫面體驗(yàn),在設(shè)置…

      2022年11月24日

    聯(lián)系我們

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