Python basics: (3) list --- basic knowledge


ps: Python uses indentation to express the relationship between them.

1. What is a list

1. Definition : A list is composed of a series of elements arranged in a specific order. The elements can be unconnected, and can be 'letters', 'numbers'....
2. Syntax : In python, the list is represented by [], the elements in it are enclosed by single quotation marks (''), and the elements are separated by commas (,).
For example: yuansu = ['0','yuansu','name']
3. Access list elements (you can refer to the c language array)

>>>yuansu = ['0','yuansu','name'] 
>>>print(yuansu)
['0', 'yuansu', 'name']
>>>print(yuansu[2])
name

insert image description here
4: The index inside [] starts from 0! ! ! (that is, the subscript starts from 0)

>>>yuansu = ['0','yuansu','name'] #也就是只有三个0,1,2,到3就会报错
>>>print(yuansu[0])
0
>>>print(yuansu[1])
yuansu
>>>print(yuansu[2])
name

insert image description here
insert image description here
5. Element operations in the list
Treat the elements in the list like variables and perform other operations, such as merging f strings, specifying element assignments, etc.

>>>yuansu = ['0','yuansu','name'] 
>>>print(yuansu)
['0', 'yuansu', 'name']
>>>yuansu[0] = 1
>>>print(yuansu)
>>>print(f"{
      
      yuansu[1]} practise")

insert image description here
tips : print(yuansu[-1]) means that the index is -1, and the last element is printed.

2. Modify, add, delete elements

2.1 Modify the value of an element

Modify using assignment statement

>>> yuansu = ['0','yuansu','name']
>>> print(yuansu)
['0', 'yuansu', 'name']
>>> yuansu[0] = 1
>>> print(yuansu)
[1, 'yuansu', 'name']

2.2 Adding new elements

2.2.1 Add at the end of the list

Replenish:

"""使用append()进行添加"""
>>> yuansu = ['1','yuansu','name']
>>> yuansu .append('python')
>>> print(yuansu)
[1, 'yuansu', 'name', 'python']

insert image description here

2.2.2 Insert an element at a specified position in the list
"""使用 insert(索引,字符串) 在指定位置添加元素"""
>>> yuansu = ['1','yuansu','name','python']
>>> yuansu.insert(0,'study')
>>> print(yuansu)
['study', 1, 'yuansu', 'name', 'python']

insert image description here

2.3 Delete elements

2.3.1 Use the del statement to delete the specified element
"""使用 del 删除指定元素"""
"""del是永久删除元素"""
>>> yuansu = ['study', '1', 'yuansu', 'name', 'python']
>>> del yuansu[0]
>>> print(yuansu)
['1', 'yuansu', 'name', 'python']

insert image description here

2.3.2 Use pop(index) to delete elements

Pop() usage background : after deleting the specified element, you want to continue to use this element
How to use it : Generally, pop is used in conjunction with an assignment statement

"""使用pop()删除末尾"""
"""使用pop(索引)删除指定元素"""
"""pop删除后可以继续使用删除的值,同赋值语句连用"""
>>> yuansu = ['1', 'yuansu', 'name', 'python']
>>> shanchu = yuansu.pop()
>>> print(shanchu)
python
>>> shanchu_1 = yuansu.pop(0)
>>> print(shanchu_1)
1
>>> print(yuansu)
['yuansu', 'name']

insert image description here

2.3.3 Delete elements based on value (remove)
"""使用 remove() 删除指定值的元素"""
"""
remove()和pop一样删除后可以继续使用删除的值;
不过注意此时需要一个中间变量进行过度
"""
>>> yuansu = ['1', 'yuansu', 'name', 'python']
>>> shanchu = '1'
>>> yuansu.remove(shanchu)
>>> print(yuansu)
['yuansu', 'name', 'python']
>>> print(shanchu)
1

insert image description here
Note : remove() only deletes the first value. If this value appears multiple times in the list, you must use a loop statement, which will be explained later.

3. Organization list (sorting)

3.1 Permanent sorting

"""使用sort()按字母顺序永久进行排序"""
>>> cars = ['audi','bmw','subaru','toyota']
>>> cars.sort()
>>> print(cars)
['audi', 'bmw', 'subaru', 'toyota']

3.2 Temporary sorting

"""使用sorted()按字母顺序临时进行排序"""
>>> cars = ['audi','bmw','subaru','toyota']
>>> print(cars)
['audi', 'bmw', 'subaru', 'toyota']
>>> print(sorted(cars))
['audi', 'bmw', 'subaru', 'toyota']
>>> print(cars)
['audi', 'bmw', 'subaru', 'toyota']

insert image description here

3.3 Reverse print list elements

"""
使用reverse()进行列表元素的反转(倒序输出)打印
这个reverse()和sort()用法一样
"""
>>> cars = ['audi','bmw','subaru','toyota']
>>> cars.reverse()
>>> print(cars)
['toyota', 'subaru', 'bmw', 'audi']
>>> cars.reverse()
>>> print(cars)
['audi', 'bmw', 'subaru', 'toyota']

ps: Two reversals are the original order
insert image description here

3.4 Determining the length of the list

"""len()函数求列表长度"""
>>> cars = ['audi','bmw','subaru','toyota']
>>> print(len(cars))
4

insert image description here

Guess you like

Origin blog.csdn.net/qq_63913621/article/details/129145673