舉例講解Python中的list列表數(shù)據(jù)結(jié)構(gòu)用法
循環(huán)和列表
不管怎樣,程序會(huì)做一些重復(fù)的事情,下面我們就用for循環(huán)打印一個(gè)列表變量。做這個(gè)練習(xí)的時(shí)候你必須自己弄懂它們的含義和作用。
在使用for循環(huán)之前,我們需要一個(gè)東西保存循環(huán)的值,最好的方法是使用一個(gè)列表,列表就是按照順序保存數(shù)據(jù)的容器,不是很復(fù)雜,就是一種新的語(yǔ)法而已,結(jié)構(gòu)像下面這樣:
hairs = ['brown', 'blond', 'red'] eyes = ['brown', 'blue', 'green'] weights = [1, 2, 3, 4]
list以 [ 號(hào)開(kāi)頭,里面的元素以 , 號(hào)分隔,像函數(shù)的參數(shù)一樣,然后以 ] 結(jié)束,python把所有這些包含在一個(gè)變量中。
下面我們來(lái)看一些list,并且循環(huán)打印它們:
the_count = [1, 2, 3, 4, 5] fruits = ['apples', 'oranges', 'pears', 'apricots'] change = [1, 'pennies', 2, 'domes', 3, 'quarters'] # this first kind of for-loop goes through a list for number in the_count: print "This is count %d" % number # same as above for fruit in fruits: print "A fruit of type: %s" % fruit # also we can go through mixed lists too # notice we have to use %r since we don't know what's in it for i in change: print "I got %r" % i # we can also build lists, first start with an empty on elements = [] # then use the range function to do 0 to 5 counts for i in range(0, 6): print "Adding %d to the list." % i # append is a function that lists understand elements.append(i) # now we can print them out too for i in elements: print "Elements was: %d" % i
運(yùn)行結(jié)果
root@he-desktop:~/mystuff# python ex32.py
This is count 1 This is count 2 This is count 3 This is count 4 This is count 5 A fruit of type: apples A fruit of type: oranges A fruit of type: pears A fruit of type: apricots I got 1 I got 'pennies' I got 2 I got 'domes' I got 3 I got 'quarters' Adding 0 to the list. Adding 1 to the list. Adding 2 to the list. Adding 3 to the list. Adding 4 to the list. Adding 5 to the list. Elements was: 0 Elements was: 1 Elements was: 2 Elements was: 3 Elements was: 4 Elements was: 5
訪(fǎng)問(wèn)列表中的元素
List是非常有用的,前提是要知道怎么用,那么我們?cè)趺丛L(fǎng)問(wèn)列表中的元素呢?下面看看我們?cè)趺丛L(fǎng)問(wèn)列表的第一個(gè)元素的:
animals = ['bear', 'tiger', 'penguin', 'zebra'] bear = animals[0]
我們使用0去獲得第一個(gè)元素?這是怎么工作的呢?因?yàn)閜ython開(kāi)始一個(gè)list是從0開(kāi)始的,看上去很奇怪,但是有很多好處,暫且認(rèn)為這是一個(gè)規(guī)定吧。
這就提現(xiàn)了我們用數(shù)字和程序用數(shù)字的不同。
想象一下,我們讓這四個(gè)動(dòng)物進(jìn)行競(jìng)速比賽,然后按照他們的名次在list排列。如果你的朋友想知道誰(shuí)贏(yíng)了,那么他會(huì)說(shuō):”誰(shuí)是第0名?“,當(dāng)然不會(huì),他會(huì)說(shuō):”誰(shuí)是第一名?“
這里也說(shuō)明了排序的重要性,沒(méi)有第一就沒(méi)有第二的說(shuō)法,沒(méi)有第二就沒(méi)有第三。而且第0名也是不可能存在的,0就表示沒(méi)有。我們?cè)趺醋寷](méi)有去贏(yíng)得比賽?這不合常理。我們把這些數(shù)字叫做有序的數(shù)字,因?yàn)樗鼈冇行虻膮^(qū)別了一些東西。
當(dāng)然,程序員不會(huì)想這些,因?yàn)樗麄兛梢詮膌ist中取出元素,對(duì)于程序員來(lái)說(shuō),上面的list就想一疊卡片。如果他們想要老虎,就取出老虎,想要斑馬就取得斑馬。想要隨機(jī)的取得任何元素的話(huà),就要給每個(gè)元素一個(gè)地址,或者說(shuō)是一個(gè)索引,最好的辦法是從0開(kāi)始,然后按照順序排列,這樣我們就能隨便取元素了,即使是第0個(gè)元素。
比如說(shuō),你想要第三個(gè)動(dòng)物,那么你就可以用3減去1,得出索引2,那么就可以得到第三個(gè)動(dòng)物了。
記?。盒蛱?hào)==順序1,基數(shù)==0
讓我們做一個(gè)練習(xí),通過(guò)我提供的序號(hào)和基數(shù)值寫(xiě)出相應(yīng)的動(dòng)物。記住,說(shuō)第一,第二的時(shí)候表示序號(hào),單單給出一個(gè)數(shù)字的時(shí)候表示基數(shù)。
animals = ['bear', 'python', 'peacock', 'kangaroo', 'whale', 'platypus']
The animal at 1. The 3rd animal. The 1st animal. The animal at 3. The 5th animal. The animal at 2. The 6th animal. The animal at 4.
回答的格式像這樣:第一個(gè)動(dòng)物是在0號(hào),它是bear。
列表操作
如果我們只知道使用list的append方法,那么我們還不是真正知道這個(gè)函數(shù)的用法,讓我們來(lái)看看它的使用方法吧。
當(dāng)我們使用mystuff.append('hello')的時(shí)候發(fā)生了一系列的事情,讓我們看看到底發(fā)生了什么:
python會(huì)先查看mystuff變量,看看是不是函數(shù)的參數(shù),或者是全局變量,反正先要找到mystuff。
當(dāng)mystuff使用 . 號(hào)的時(shí)候,會(huì)先看mystuff這個(gè)變量是什么,如果是list的話(huà),會(huì)有很多它的方法可以使用。
使用append的時(shí)候,會(huì)去查找mystuff是不是有'append‘這個(gè)名稱(chēng),有的話(huà)就直接拿來(lái)使用。
如果append后面有圓括號(hào),那么就知道它是一個(gè)函數(shù),像平時(shí)一樣執(zhí)行這個(gè)函數(shù)就好了,還會(huì)有額外的參數(shù)。
這個(gè)額外的參數(shù)就是mystuff,奇怪吧,但是python就是這樣做的,最后函數(shù)是這樣的:append(mystuff, 'hello')。
大多數(shù)情況下你不必知道這是怎么回事,但是在你遇到下面這個(gè)錯(cuò)誤的時(shí)候就有幫助了:
root@he-desktop:~# python Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information.
>>> class Thing(object): ... def test(hi): ... print "hi" ... >>> a = Thing() >>> a.test("hello") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: test() takes exactly 1 argument (2 given) >>>
下面的練習(xí)是將字符串和列表混合起來(lái),看看我們能不能找出一些好玩的。
ten_things = "Apples Oranges Crows Telephone Light Sugar" print "Wait there's not 10 things in that list, let's fix that." stuff = ten_things.split(" ") more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"] while len(stuff) != 10: next_one = more_stuff.pop() print "Adding:", next_one stuff.append(next_one) print "Thing's %d items now." % len(stuff) print "There we go:", stuff print "Let's do some things with stuff." print stuff[1] print stuff[-1] # print stuff.pop() print ' '.join(stuff) print '#'.join(stuff[3:5])
運(yùn)行結(jié)果
root@he-desktop:~/mystuff# python ex38.py
Wait there's not 10 things in that list, let's fix that. Adding: Boy Thing's 7 items now. Adding: Girl Thing's 8 items now. Adding: Banana Thing's 9 items now. Adding: Corn Thing's 10 items now. There we go: ['Apples', 'Oranges', 'Crows', 'Telephone', 'Light', 'Sugar', 'Boy', 'Girl', 'Banana', 'Corn'] Let's do some things with stuff. Oranges Corn Corn Apples Oranges Crows Telephone Light Sugar Boy Girl Banana Telephone#Light
相關(guān)文章
詳解pytest分布式執(zhí)行插件?pytest-xdist?的高級(jí)用法
這篇文章主要介紹了pytest分布式執(zhí)行插件?pytest-xdist?的高級(jí)用法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08超詳細(xì)PyTorch實(shí)現(xiàn)手寫(xiě)數(shù)字識(shí)別器的示例代碼
這篇文章主要介紹了超詳細(xì)PyTorch實(shí)現(xiàn)手寫(xiě)數(shù)字識(shí)別器的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Python編程實(shí)現(xiàn)雙擊更新所有已安裝python模塊的方法
這篇文章主要介紹了Python編程實(shí)現(xiàn)雙擊更新所有已安裝python模塊的方法,涉及Python針對(duì)模塊操作命令的相關(guān)封裝與調(diào)用技巧,需要的朋友可以參考下2017-06-06SELENIUM自動(dòng)化模擬鍵盤(pán)快捷鍵操作實(shí)現(xiàn)解析
這篇文章主要介紹了SELENIUM自動(dòng)化模擬鍵盤(pán)快捷鍵操作實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10numpy.ndarray 實(shí)現(xiàn)對(duì)特定行或列取值
今天小編就為大家分享一篇numpy.ndarray 實(shí)現(xiàn)對(duì)特定行或列取值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12