免费爱碰视频在线观看,九九精品国产屋,欧美亚洲尤物久久精品,1024在线观看视频亚洲

      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)

      要將其他列表中的元素附加到當前列表,請使用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()方法,默認情況下,該方法將按字母數(shù)字升序?qū)α斜磉M行排序

      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)元素的當前排序順序。

      thislist = [“banana”, “Orange”, “Kiwi”, “cherry”]thislist.reverse()print(thislist)

      有多種方法可以復(fù)制,一種方法是使用內(nèi)置的列表方法copy()。

      您不能簡單地通過鍵入list2=list1復(fù)制列表,因為:list2將僅是對list1的引用,并且在list1中所做的更改也將自動在list2中進行。

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

      制作副本的另一種方法是使用內(nèi)置方法list()。

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

      在Python中,有幾種方法可以連接或串聯(liá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)

      要連接兩個或多個元組,可以使用+運算符

      tuple1 = (“a”, “b” , “c”)tuple2 = (1, 2, 3)tuple3 = tuple1 + tuple2print(tuple3)

      如果要將元組的內(nèi)容乘以給定的次數(shù),可以使用*運算符

      fruits = (“apple”, “banana”, “cherry”)mytuple = fruits * 2print(mytuple)

      集合

      創(chuàng)建集后,不能更改其項,但可以添加新項。

      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)

      字典

      您可以通過在方括號內(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之前的版本中,將刪除隨機項)

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

      您不能簡單地通過鍵入dict2=dict1來復(fù)制字典,因為: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}

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

      相關(guān)推薦

      • 注意了!當前最不值得買的幾款相機

        不得不說,這兩年,相機發(fā)布的頻率越來越低。以前,旺季一個月都有十幾款,現(xiàn)在一年下來,稱得上重量級的,能被人記住的,也就十款左右。 經(jīng)常有粉絲來問我,買什么好?我就納悶了,你這機身才…

        2022年8月2日
      • 金融科技:虛擬貨幣

        最近一個新詞在IT圈子里頗為火爆——金融科技( FinTech,F(xiàn)inance Technology )。金融科技其實早就有,而且在交易領(lǐng)域向來是新科技的試驗場,因為它離錢最近。 …

        2022年6月28日
      • 微信語音鈴聲怎么設(shè)置歌曲自定義(微信語音視頻鈴聲修改教程)

        微信是我們?nèi)粘V辛奶觳豢缮俚墓ぞ咧?,?jīng)常用來聊天或者發(fā)朋友圈等。有網(wǎng)友的微信語音來電沒有鈴聲提醒,有時候經(jīng)常錯過好友的語音來電,因此想了解微信鈴聲怎么設(shè)置。下面小編就教下大家設(shè)置…

        2022年5月1日
      • 入門機賣6000元你接受嗎?尼康Z 30微單相機定價曝光

        此前我們曾報道了尼康將會在本月29日召開新品發(fā)布會,并正式發(fā)布Z 30微單相機和尼克爾Z 400mm f/4.5 VR S定焦鏡頭,而今日關(guān)于這兩款新產(chǎn)品的英國官方售價已經(jīng)曝光,Z…

        2022年6月29日
      • 別再把“白褲子”穿錯了,牢記這“3要3不要”,保準顯瘦又洋氣

        基礎(chǔ)款褲型中,白褲子一直是最備受爭議的系列。會穿懂穿的姐妹們說它是凹造型的絕佳“武器”,不會穿的女人又對它避之不及。所以,白褲子到底怎么穿,才是正確的打開方式呢? 今天特別為大家整…

        2022年6月21日
      • 皮面勞保勞保鞋的保養(yǎng)方式

        關(guān)于勞保勞保鞋的材質(zhì),我們都知道天然皮或者是人工皮的材質(zhì)比較常見也比較適合做勞保勞保鞋,無論是哪種材質(zhì)的勞保勞保鞋都需要日常保養(yǎng),接下來小編給大家介紹一下關(guān)于皮面勞保勞保鞋的保養(yǎng)方…

        2022年6月3日
      • Meta Platforms正在削減成本,停止生產(chǎn)兩個攝像頭的原型手表

        一位消息人士透露,F(xiàn)acebook 的母公司 Meta Platforms ( META )阻止了一款配備雙攝像頭的智能手表的開發(fā)——Get Meta Platforms Inc.…

        2022年6月14日
      • 華為良心了!Mate50起售價曝光,5099元只比上代漲價100塊?

        華為Mate50系列定檔9月6日發(fā)布,據(jù)傳比iPhone14系列提前一天亮相,針鋒相對的味道非常明顯,分析師郭明錤認為價格成為影響銷量的主要因素。盡管現(xiàn)在還處于嚴格保密期間,仍然有…

        2022年8月24日
      • 長痘痘怎么調(diào)理?

        長痘痘其實可以采用內(nèi)服的方式加上生活作息的調(diào)理來進行改善,吃一些清熱解毒的食物、選擇一些濕和性高的沐浴產(chǎn)品、多喝水、多運動這些方式都可以很好的改善病情,同時也服用一些金銀花、枸杞、…

        2022年8月26日
      • 買過的能不能跟我說說,奇瑞捷豹怎么賣?

        坐標成都,買的是捷豹XFL,花了大概近40萬。我個人感覺這價格真的超值了,操控性強、動力足,空間方面也沒啥問題,駕乘感都是頂級的,真的沒得挑~ 奇瑞捷豹?題主問的是奇瑞捷豹路虎推出…

        2022年7月26日

      聯(lián)系我們

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