4、python入門 列表(List) ——Mosh

列表,[ ]

一、一維列表

列表是最常用的Python數據類型之一,它可以作爲一個方括號內的逗號分隔值出現。

列表的數據項可以有不同的數據類型

創建一個列表,只要把逗號分隔的不同的數據項使用方括號括起來即可。

list1 = ['Google', 'Runoob', 1997, 2000]
print(list1)

>>>['Google', 'Runoob', 1997, 2000]

與字符串的索引一樣,列表索引從0開始。列表可以進行截取、組合等。

1、訪問列表中的值

使用下標索引來訪問列表中的值(就像字符串一樣,訪問所返回的列表爲新列表,不改變原來列表的值)

names = ['John','Bob','Mosh','Sarah','Mary']
print(names[2:])
print(names)

>>>['Mosh', 'Sarah', 'Mary']
>>>['John', 'Bob', 'Mosh', 'Sarah', 'Mary']

2、修改列表的值(更新列表)

list[index] = value

names = ['John','Bob','Mosh','Sarah','Mary']
names[0] = 'Jon'
print(names)

>>>['Jon', 'Bob', 'Mosh', 'Sarah', 'Mary']

Exercise:簡單排序

numbers = [3, 6, 2, 8, 4, 10]
max = numbers[0]
for number in numbers:
    if max < number:
        max = number
print(max)
>>>10

二、二維列表

外部列表的每一項都是另一個列表
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
訪問和修改列表的值與一維列表大同小異,區別在於有兩個索引

matrix = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
print(matrix[0][1])
>>>2

#可用嵌套循環來遍歷二位列表
matrix = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
for row in matrix:
    for item in row:
        print(item)

>>>
1
2
3
4
5
6
7
8
9

三、列表函數

1、增

list.append(object)

在列表的末尾添加項目

numbers = [5, 2, 1, 7, 4]
numbers.append(20)
print(numbers)
>>>[5, 2, 1, 7, 4, 20]

numbers = [5, 2, 1, 7, 4]
numbers.append('apple')
print(numbers)
>>>[5, 2, 1, 7, 4, 'apple']

list.insert(index, object)

任意位置添加

numbers = [5, 2, 1, 7, 4]
numbers.insert(0,'apple')
print(numbers)
>>>['apple', 5, 2, 1, 7, 4]

2、刪

list.remove(object)

刪除某一項

numbers = [5, 2, 1, 7, 4]
numbers.remove(1)
print(numbers)
>>>[5, 2, 7, 4]

list.clear()

清空列表
語法:list.clear(),無參數

numbers = [5, 2, 1, 7, 4]
numbers.clear()
print(numbers)
>>>[]

list.pop()

刪除列表中最後一項

numbers = [5, 2, 1, 7, 4]
numbers.pop()
print(numbers)
>>>[5, 2, 1, 7]

3、查

list.index(object)

返回所查元素的索引值

numbers = [5, 2, 1, 7, 4]
print(numbers.index(1))
>>>2

如果該元素不存在,則會報錯。可以根據這個來查找某個列表中是否在某個元素。

操作符in

返回布爾值(Ture or False)
類比檢查字符串中字符的存在

numbers = [5, 2, 1, 7, 4]
print(50 in numbers)
>>>False

list.count(object)

查找object在列表中存在的個數

numbers = [5, 2, 1, 5, 7, 4]
print(numbers.count(5))
>>>2

4、排序

升序 list.sort()

numbers = [5, 2, 1, 5, 7, 4]
numbers.sort()
print(numbers)
>>>[1, 2, 4, 5, 5, 7]

倒序 list.reverse()

注意:list.reverse()並非降序,而是倒序,從列表的最後一個元素變成第一個元素,倒數第二個元素變成第二個元素…以此類推

numbers = [5, 2, 1, 5, 7, 4]
numbers.reverse()
print(numbers)
>>>[4, 7, 5, 1, 2, 5]

如果要實現降序,則可以先將列表升序 list.sort(),再用倒序 list.reverse()實現。

numbers = [5, 2, 1, 5, 7, 4]
numbers.sort()
numbers.reverse()
print(numbers)
>>>[7, 5, 5, 4, 2, 1]

list.copy()

拷貝,得到的是完全全新的一個列表,拷貝之後,再對原列表進行任何操作都不會影響到這個新列表

numbers = [5, 2, 1, 5, 7, 4]
numbers2 = numbers.copy()
numbers.append(10)
print(f'numbers is: {numbers}')
print(f'numbers2 is: {numbers2}')
>>>numbers is: [5, 2, 1, 5, 7, 4, 10]
>>>numbers2 is: [5, 2, 1, 5, 7, 4]

Exercise

刪除列表中的重複元素

numbers = [2, 2, 4, 6, 3, 4, 6, 1]
uniques = []
for number in numbers:
    if number not in uniques:
        uniques.append(number)
print(uniques)
>>>[2, 4, 6, 3, 1]

如果要求比較嚴格,要使numbers這個列表真正變爲無重複元素,則可在for循環後面再加

numbers = uniques.copy()

並打印numbers即可

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