Python入門 -- List

今天讀了“Head First Python”, 很不錯的入門書,讀起來非常的有趣,推薦給剛剛接觸python的同學。


List是python裏面用到的最多的數據類型。


List的總結:

1. List是用方括號括起來的。eg. fav_fruit = ["watermelon", "strawberry", "kiwi", "avocado", "cucumber"] 或者是fav_fruit = [‘watermelon’, ‘strawberry’, ‘kiwi’, 'avocado', 'cucumber'] 也可以是雙引號(double quotes)和單引號(signal quotes)混合的 fav_fruit = ["watermelon", ‘strawberry’, "kiwi",'avocado', "cucumber"]   這就是一個list了,裏面都是我愛的水果。

    想要輸出這個list的話用的語句是:print fav_fruit 或者是 print(fav_fruit)

    得到的結果是:[‘watermelon’, ‘strawberry’, ‘kiwi’, 'avocado', 'cucumber']

    

實例:>>fav_fruit = ['watermelon', 'strawberry', 'kiwi', 'avocado', 'cucumber']

               >>print (fav_fruit[0])

               Output:[‘watermelon’, ‘strawberry’, ‘kiwi’, 'avocado', 'cucumber']


2. python中的list和C/C++中的array很相似,都是下標從0開始, 但是不同的是python的list有負索引, 最後一個元素的下標可以是 -1

      實例:>> print (fav_fruit[0])

                 Output: watermelon

                 >>print (fav_fruit[-1])

                 Output: cucumber

                 >>print (fav_fruit[-3])

                Output: kiwi

                >>print (fav_fruit[2])

                Output: kiwi


正索引和負索引之間存在一個關係:    list [ - n ] = list [ len ( list ) - n ]

 

3. pop()改變了原有的 list

    pop()函數是刪掉 list 的最後一個元素並且返回這個元素:

    實例:>>fav_fruit.pop()

                Output: 'cucumber'

               >>print (fav_fruit)

               Output: fav_fruit = ['watermelon', 'strawberry', 'kiwi', 'avocado']


4. append(one argument, this argument can be any type, like int, string, list and ect.)是在list 的結尾添加元素,改變了list,但是並不會返回值。

    實例: >>fav_fruit.append("cucumber")

                >>print fav_fruit

                Output: [‘watermelon’, ‘strawberry’, ‘kiwi’, 'avocado', 'cucumber']

 

    append一次只可以添加一個元素:

   實例: >>fav_fruit.append("banana", "apple")

                TypeError: append() takes exactly one argument (2 given)

               但是可以添加一個list:

              >>fav_fruit.append(["banana", "apple"])

               Output: fav_fruit = ['watermelon', 'strawberry', 'kiwi', 'avocado', 'cucumber', 'banana', 'apple']               


5, insert(two arguments, one is index, the other is element) 將單個的元素添加到 list 中。index表示的是插入元素的下標。改變了list,但是並不會返回值。

    List 裏面的元素不必是唯一的,他的元素可以重複。

   實例: >>fav_fruit =  ['watermelon', 'strawberry', 'kiwi', 'avocado', 'cucumber']

               >> fav_fruit.insert (2, 'watermelon')

               >>print fav_fruit

               Output: ['watermelon', 'strawberry', 'watermelon', 'kiwi', 'avocado', 'cucumber']


6. extend(one argument, this argument only can be list or string)

     >> a = [1, 2]

     >> a.extend(3)

     TypeError: 'int' object is not iterable


    >> a.extend("3")

    >>print a

    Output: [1, 2, '3']


   >>a.extend([4, 5])

  >>print a

   Output: [1, 2, '3', 4, 5]

7. remove(): 從list中刪除首次出現的某個元素

實例:  >> a = [1, 2, 3, 1, 2, 3]

              >>a.remove(2)

              >> print a

             Output: [1, 3, 1, 2, 3]


8. +: 可以連接兩個list

實例:  >> a = [1, 2, 3]

              >>b = [4, 5]

              >> print a + b

             Output: [1, 2, 3, 4, 5]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章