国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

舉例講解Python中的list列表數據結構用法

2020-01-04 17:38:13
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了Python中的list列表數據結構用法,列表是Python內置的六種集合類數據類型中最常見的之一,需要的朋友可以參考下
 

循環和列表

不管怎樣,程序會做一些重復的事情,下面我們就用for循環打印一個列表變量。做這個練習的時候你必須自己弄懂它們的含義和作用。

在使用for循環之前,我們需要一個東西保存循環的值,最好的方法是使用一個列表,列表就是按照順序保存數據的容器,不是很復雜,就是一種新的語法而已,結構像下面這樣:

hairs = ['brown', 'blond', 'red']eyes = ['brown', 'blue', 'green']weights = [1, 2, 3, 4]

list以 [ 號開頭,里面的元素以 , 號分隔,像函數的參數一樣,然后以 ] 結束,python把所有這些包含在一個變量中。

下面我們來看一些list,并且循環打印它們:

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 


運行結果

root@he-desktop:~/mystuff# python ex32.py 
This is count 1This is count 2This is count 3This is count 4This is count 5A fruit of type: applesA fruit of type: orangesA fruit of type: pearsA fruit of type: apricotsI got 1I got 'pennies'I got 2I got 'domes'I got 3I 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: 0Elements was: 1Elements was: 2Elements was: 3Elements was: 4Elements was: 5


訪問列表中的元素
List是非常有用的,前提是要知道怎么用,那么我們怎么訪問列表中的元素呢?下面看看我們怎么訪問列表的第一個元素的:

animals = ['bear', 'tiger', 'penguin', 'zebra']bear = animals[0]

我們使用0去獲得第一個元素?這是怎么工作的呢?因為python開始一個list是從0開始的,看上去很奇怪,但是有很多好處,暫且認為這是一個規定吧。

這就提現了我們用數字和程序用數字的不同。

想象一下,我們讓這四個動物進行競速比賽,然后按照他們的名次在list排列。如果你的朋友想知道誰贏了,那么他會說:”誰是第0名?“,當然不會,他會說:”誰是第一名?“

這里也說明了排序的重要性,沒有第一就沒有第二的說法,沒有第二就沒有第三。而且第0名也是不可能存在的,0就表示沒有。我們怎么讓沒有去贏得比賽?這不合常理。我們把這些數字叫做有序的數字,因為它們有序的區別了一些東西。

當然,程序員不會想這些,因為他們可以從list中取出元素,對于程序員來說,上面的list就想一疊卡片。如果他們想要老虎,就取出老虎,想要斑馬就取得斑馬。想要隨機的取得任何元素的話,就要給每個元素一個地址,或者說是一個索引,最好的辦法是從0開始,然后按照順序排列,這樣我們就能隨便取元素了,即使是第0個元素。

比如說,你想要第三個動物,那么你就可以用3減去1,得出索引2,那么就可以得到第三個動物了。

記住:序號==順序1,基數==0

讓我們做一個練習,通過我提供的序號和基數值寫出相應的動物。記住,說第一,第二的時候表示序號,單單給出一個數字的時候表示基數。

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.

回答的格式像這樣:第一個動物是在0號,它是bear。

列表操作
如果我們只知道使用list的append方法,那么我們還不是真正知道這個函數的用法,讓我們來看看它的使用方法吧。

當我們使用mystuff.append('hello')的時候發生了一系列的事情,讓我們看看到底發生了什么:
python會先查看mystuff變量,看看是不是函數的參數,或者是全局變量,反正先要找到mystuff。
當mystuff使用 . 號的時候,會先看mystuff這個變量是什么,如果是list的話,會有很多它的方法可以使用。
使用append的時候,會去查找mystuff是不是有'append‘這個名稱,有的話就直接拿來使用。
如果append后面有圓括號,那么就知道它是一個函數,像平時一樣執行這個函數就好了,還會有額外的參數。
這個額外的參數就是mystuff,奇怪吧,但是python就是這樣做的,最后函數是這樣的:append(mystuff, 'hello')。
大多數情況下你不必知道這是怎么回事,但是在你遇到下面這個錯誤的時候就有幫助了:

root@he-desktop:~# pythonPython 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3] on linux2Type "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)>>> 

下面的練習是將字符串和列表混合起來,看看我們能不能找出一些好玩的。

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]) 


運行結果

root@he-desktop:~/mystuff# python ex38.py 
Wait there's not 10 things in that list, let's fix that.Adding: BoyThing's 7 items now.Adding: GirlThing's 8 items now.Adding: BananaThing's 9 items now.Adding: CornThing's 10 items now.There we go: ['Apples', 'Oranges', 'Crows', 'Telephone', 'Light', 'Sugar', 'Boy', 'Girl', 'Banana', 'Corn']Let's do some things with stuff.OrangesCornCornApples Oranges Crows Telephone Light Sugar Boy Girl BananaTelephone#Light

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亳州市| 嘉禾县| 信阳市| 湘潭市| 双流县| 浮山县| 清徐县| 卢氏县| 达尔| 宜章县| 武穴市| 滦平县| 林甸县| 江门市| 临颍县| 怀来县| 桃园县| 宁晋县| 儋州市| 射阳县| 南木林县| 瑞丽市| 开封市| 鸡东县| 龙门县| 江北区| 九台市| 青神县| 阿拉善左旗| 枞阳县| 溆浦县| 蒲江县| 青岛市| 临江市| 昌江| 长岛县| 新宾| 柞水县| 如皋市| 玉树县| 高雄市|