Python native data structure - use of list (list)


1. What is a list?

A list needs to use square brackets [ ] to frame the various data in it, and each data in it is called an "element". Each element must be separated by a comma.

A list is an ordered collection in which elements can be added and removed at any time.

2. list creation

To create a one-dimensional list, use square brackets [ ] to create a list object.

The creation of multidimensional lists, although list is one-dimensional by default, in practical applications we can create multidimensional lists as needed. The creation of multi-dimensional lists can use **[ ] ** nesting.

the code

students=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
empty=[]
print("students的元素是",students,
     "\n显示该函数数据结构类型",type(students),
     "\nempty的元素是",empty)

operation result

insert image description here

lists=[[1,2],['a','b']]
print("多维列表:\n",lists)

operation result
insert image description here

3. list query operation

Index the list by offset (which can be understood as search positioning), and read the elements we need.

Note: 1. The offset starts from 0 instead of 1 as we are used to; 2. Add square brackets with offset after the list name to get the element at the corresponding position.

print("\n列表切分,从列表中取第2个到第7个元素:",students[1:8], 
     "\n固定步长2访问:",students[::2],
     "\n从后往前访问,取倒数第3个元素:",students[-3])

operation result
insert image description here

4. Add operation to list

For the increase of the list, **append()** can add new items at the end of the list, add an element, or add a list pair to become a multidimensional list.

students.append(11)
print("增加一个元素后的列表",students)

lists.append(["hello",5])
print("增加列表后的列表",lists)

operation result
insert image description here

5. List delete operation

remove() function : Delete a single element with a specified value, delete the first element that meets the conditions, and delete by value. list.remove(i) will delete the element whose value is i in the list object, and report an error if it does not exist.

pop() function : delete single or multiple elements, delete bit by bit (delete according to the index), the default is the last element of the list object, list.pop(i) deletes the element with the subscript i, and returns deleted when deleting Elements.

del function : delete the fragment (value in the specified range) in the list or clear the entire list. It is deleted according to the index (where the element is located).

students=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
lists=[[1,2],['a','b']]
list_2=[1,2,3,4,5,6]

students.remove(5)
lists.pop()
del list_2[1]

print("删除students列表第1个元素为5的列表对象\n",students,
     "\n删除lists列表的最后1个元素的列表对象\n",lists,
     "\n删除列表str第2个元素的列表对象\n",str)   

operation result
insert image description here

list_2=[1,2,3,4,5,6]
del list_2[2:4] #删除从第3个元素开始到第5个元素为止(但是不包括尾部元素)
list_2

operation result
insert image description here

list_2=[1,2,3,4,5,6]
del list_2#删除整个列表
list_2

6. List modification operation

List modification, list[i]=x can directly replace the element of the specified subscript in the list.

print("修改前的列表为",students)
students[0]=100
print("\n修改后的列表为",students)

operation result

insert image description here

7. list special operations

reverse() function : the list can be inverted.
len() function : It can return the number of elements in the list.
sort() function : It can arrange the list elements in ascending order.

students.reverse()
print("\n倒置后的列表是",students,
     "\nlen统计列表元素的个数",len(students))
students.sort()
print("sort列表升序排列",students)

operation result
insert image description here

8. Example of converting list to other data structures

List data type conversion, as one of the most commonly used data types, lists can be easily converted to various data types. But again, a single list cannot be directly converted into a dictionary.

print("\n列表转元组:\n",tuple(students),
     "\n列表转字符串:\n",str(students))

operation result
insert image description here

Guess you like

Origin blog.csdn.net/sodaloveer/article/details/129561152
Recommended