昨天,我們談論了list跟tuple的異同,並帶大家體驗了一些關於list的操作。今天,我們將接續昨天的內容,為大家帶來其他list的相關內容唷!
1.從list中移除資料(by 位置)
如同我們之前所述,每個list裡面的資料都是有順序的。那麼,我們怎麼移除某個位置的資料呢? 用pop(位置)就對了!

然後,我們也可以在pop()裡面放入指定的位置,執行後將會移除指定位置之資料。
2.從list中移除資料(By資料值)
除了用位置移除資料外,我們也可以用remove(指定資料值)藉由指定資料的值來移除資料。值得注意的是,當有兩筆相同的指定資料,執行後只會移除第一筆指定的資料。

3.複製list
複製list有兩種常用的方法,一種是直接採用copy(),另外一種則是用我們昨天教的,指定新的變數等於舊變數的全部資料,都可以成功複製唷!


然而,要注意的是,如果是想複製(就是還要保存舊的資料的話),千萬不可以直接指定新的變數等於舊的變數唷!不然這樣未來在修改新的變數資料時,舊的資料也會被修改到噢!

4.數值list
以上我們所舉的範例,主要都是以字串list為主。所謂的數值list,其實指的就是list裡面都是數字。而我們之前所教的所有關於list的功能,都適用於數值list。
另外,我們還可以用max()、min()以及sum()等功能,直接找出list的最大值、最小值以及和唷!

5.利用range()自動建立有順序的list
在Python中,如果我們想要建立有順序的list,不用一個一個慢慢輸入,直接用range(開頭,結尾,間隔)就可以囉!不過要記得,這個range所取的數值一樣是含頭不含尾唷!

(二) Tuple
接下來,讓我們來談談Tuple吧!其實Tuple跟List一樣,裡面都可以放入各式各樣的資料。然而,兩者之間最大的不同在於,Tuple裡面的資料內容是無法被更動的。所以我們上述所教關於list的各種功能,只要有更動到資料內容,對於Tuple來說都是不適用的。而Tuple資料結構的最大好處,就是可以防止使用者不小心更動到資料的內容。(因為資料皆不可修改!)
1.建立tuple
在Python中,我們用小括號”( )”來建立Tuple。

同樣的,我們也可以用range()來建立有順序的數值Tuple。

2.Tuple資料的不可修改性
前面我們提到,Tuple裡面的資料是不可修改的。因此,如果我們想要對裡面資料作任何更動,將會產生錯誤。

到這邊,相信大家已經對於兩大資料結構list與tuple有了更多的認識,希望大家有時間多多練習,多多熟悉唷!
明天,我們將帶大家看到第三種資料結構:dictionary!敬請期待!
I’m commenting to let you be aware of of the beneficial discovery my child developed browsing the blog. She came to find a lot of details, including how it is like to have a great coaching character to get certain people completely comprehend a number of complex issues. You actually did more than her desires. I appreciate you for coming up with such essential, dependable, edifying and cool guidance on that topic to Ethel.