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即可

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