python data structure-list-1

Python data structure-list:
Variables can store one element, but the list is a "big container" that can store N elements, which can facilitate the overall operation of these data.
Lists are equivalent to arrays in other languages:

Creation of the list:

#First 使用[]进行创建
List = ["I","Love","Python"]

#Second 使用list()方法进行创建
List = list(["I","Love","Python"])

List operation

List = ["I","Love","Python"]

print(List.index("Love"))

#输出结果为1

1. If there are multiple identical elements in the list, only the index value of the first element is returned

2. If the queried element does not exist, it returns ValueError

3. You can also search within the specified range

List = ["I","Love","Python"]

print(List.index("Love",0,1))

#输出结果为1,在0到1之间查找

1. Forward index

List = ["I","Love","Python"]

print(List[2])

#输出结果为Python

2. Reverse index

List = ["I","Love","Python"]

print(List[-1])

#输出结果为Python

3. If the element does not exist, an IndexError will be thrown.
1. Get the list name by slicing [start, stop, step]

List = ["I","Love","Python"]

print(List[0:2:1])
#输出结果为['I','Love']

#由于步长可以省略,所以step可以不写
print(List[0:2])
#输出结果依然为['I','Love']

print(List[0:3:2])
#设置步长为2.输出结果为['I','Python']

print(List[2:0:-1])
#若设置步长为-1,则start为最后一个元素,stop默认为第一个元素
#输出结果为['Python','Love']

It should be noted that the slicing operation will create a new list object, that is, the id (location in memory) of the object will change

Determine whether the list element exists

Added operations of class table elements

1.append() 在列表的末尾添加一个元素
2.extend() 在列表的末尾至少添加一个元素
3.insert() 在列表的任意位置添加至少一个元素
4.切片   在列表的任意位置添加一个一个元素

append() function

List = ['I','Love','Python']
List.append('Very Much')
print(List)
#输出结果为['I','Love','Python','Very Much']

However, if you want to add two elements ['Very','Much'], use the append() function to insert the two elements into the list as a list element

List = ['I','Love','Python']
List.append(['Very','Much'])
print(List)
#则会输出['I','Love','Python',['Very','Much']]

extend() function
Use the entend() function to avoid the above situation

List = ['I','Love','Python']
List.extend(['Very','Much'])
print(List)
#则会输出['I','Love','Python','Very','Much']

The insert() function
will insert the element at the specified position

List = ['I','Love','Python']
List.insert(3,"Very Much")
print(List)
#输出结果为 ['I', 'Love', 'Python', 'Very Much']

Use slices to replace

List = ['I', 'Love', 'Python']
List[2:] = ["C++"]
print(List)
#输出结果为 ['I', 'Love', 'C++']

Deletion of list elements

删除操作:
remove 
一次删除一个元素
重复元素只删除第一个
元素不存在就抛出ValueError
--------
pop
删除一个指定索引位置上的元素
指定索引不存在抛出indexError
不指定索引,删除列表中的最后一个元素
--------
切片 
一次至少删除一个元素
--------
clear()
清空列表
--------
del
删除列表
--------


remove() function

List = ['I', 'Love', 'Python']
List.remove('I')
print(List)
#输出结果为['Love','Python']

pop() function

List = ['I', 'Love', 'Python']
List.pop(0)
print(List)
#输出结果为['Love','Python']

slice

List = ['I', 'Love', 'Python']
List[1:] = []
#默认stop为最后一个元素,设置从索引值为1的元素Love到最后用[]代替

print(List)
#输出结果为['I']

The claer() function
clears the list

List = ['I', 'Love', 'Python']
List.clear()
#执行clear()方法

print(List)
#输出结果为[],输出结果为空列表

del() function
delete list

List = ['I', 'Love', 'Python']
del List
#执行del 内置函数

print(List)
# 报错:NameError: name 'List' is not defined

List element modification
Assign a new value to the element at the specified index

List = ['I', 'Love', 'Python']
List[2] = 'C++'

print(List)
#输出结果为List = ['I', 'Love', 'C++']

Assign a new value to the specified slice

List = ['I', 'Love', 'Python']
List[2:] = ['C++']

print(List)
#输出结果为List = ['I', 'Love', 'C++']

Sort the list elements
sort()
calls the sort() method, so that the elements in the list are sorted in ascending order; if you want to sort them in descending order, set reverse = True

List = [1,2,6,7,3,4,5]

List.sort()
print(List)
#输出结果为[1, 2, 3, 4, 5, 6, 7]

List.sort(reverse=True)
print(List)
#输出结果为[7, 6, 5, 4, 3, 2, 1]

sorted() built-in function

List = [1,2,6,7,3,4,5]

print(sorted(List))

#输出结果为[1, 2, 3, 4, 5, 6, 7]

Guess you like

Origin blog.csdn.net/wtt234/article/details/114253285