Self-study Python in 20 days with no basic knowledge | Day9 List list usage encyclopedia

Hello everyone, my name is Ning Yi.

Today we will talk about Python lists.

The list is used to save a set of data, use square brackets [ ] to wrap the data, and separate the data with English commas. For example: [1,2,3,4,5]

1. List features

I gave you an example in the previous course.

If the list is a young lady and the data in the list are dolls, it can be described as follows:

2. Create a list

The data items in the list can be of different data types.

For example, if we create a list named ningyi, we can put information such as strings and integers in a list.

ningyi =  ["女",26,"1995-07-12"]

3. Find list elements

Each element in the list is assigned a number, which we usually call an "index". The index of the list starts from 0, the first index is 0, the second index is 1, and so on...

The acquisition method is actually the same as the string we talked about in the previous lesson.

# 获取第1个元素
ningyi[0]     # 输出 '女'


# 获取第2个元素
ningyi[1]     # 输出 26

You can also use the index to get a range of elements, be careful not to include the last character.

# 获取前面两个元素
ningyi[0:2]    # 输出 ['女', 26]
ningyi[:2]    # 这样写也可以,将0省略


# 获取后面两个元素
ningyi[1:3]   # 输出  [26, '1995-07-12']
ningyi[-2:] # 这样写也可以,用负数索引

4. Update list elements

(1) append() adds new elements to the end of the list

ningyi.append(55)
# 输出   ['女', 26, '1995-07-12', 55]

(2) insert() adds a new element at a specific index position

# 在索引为3的位置,添加44这个元素
ningyi.insert(3,44)
# 输出   ['女', 26, '1995-07-12', 44, 55]

(3) extend() merges two lists

list1 = [1,2,3]
list2 = [4,5,6]


list1.extend(list2) 
# 输出   [1, 2, 3, 4, 5, 6]


#可以直接用+号合并
list1+list2

5. Delete list elements

(1) del deletes elements at a specific index

Delete the element 44 we added above at index 3.

del ningyi[3]
# 输出   ['女', 26, '1995-07-12', 55]

(2) pop() deletes the last element

Remove and return the last element.

ningyi.pop()
# 输出   55

(3) remove() deletes elements based on value

ningyi.remove("女")
# 输出   [26, '1995-07-12']

Note: The remove() function will only remove the first occurrence in the list. If you want to remove all matching elements. It can be operated with a while loop, which we will learn later in the course.

(4) clear() clears the list

# 清空ningyi列表
ningyi.clear()

6. Other common operations

We redefine a list named list1:

list1 = [3,4,5,1,2,3]

(1)len() list length

len(list1)
# 输出   6

(2) in list judgment

in is used to determine whether a value exists in the list and returns True or False.

7 in list1
# 输出 False
1 in list1
# 输出 True

You can also use not in, the output is just the opposite of in.

1 in list1
# 输出 False

(3) count() counts the number of times an element appears in the list

list1.count(4)
# 输出 1

(4) index() gets the index of an element

list1.index(4)
# 输出 1

(5) Maximum value and minimum value of list elements

Note that the elements in the list can only be of numeric type.

max(list1)
# 输出   5


min(list1)
# 输出   1

(6)sort() sorting

The default is to sort from small to large.

list1.sort()
# 输出   [1, 2, 3, 3, 4, 5]

(7) reverse() list flip

It is generally used together with the sort() sorting function. sort() defaults to sorting from small to large. Combined with reverse(), it can be used to sort from large to small.

list1.reverse()
# 输出 [5, 4, 3, 3, 2, 1]

Click to follow and get started with Python without getting lost~

Guess you like

Origin blog.csdn.net/shine_a/article/details/126658509