05 Python list and tuple slicing operations

05 python lists and tuples

List

The list is an ordered collection of elements, all elements are placed in a pair of [] , separated by, and no length limit
. The index of the list starts with 0 bit, -1 means the position at the end of the
list can be spliced ​​with +, * means Duplicate When
list elements are added or deleted, the list object will automatically expand or memory shrink to ensure that there are no gaps between the elements

List elements can access individual elements by index, just like arrays
eg a[3]

The list can complete the realization of most collection data structures
. The element types in the list can be different. It supports numbers, strings, tuples, collections, dictionaries, etc. and even lists (nested)

Modification of list elements:

The size of the list is not limited, it can be modified at any time, and the element can also be modified at any time
a.insert(0,1) Insert 1 at position 0 and
modify a[0]=10

Basic operation of the list (assuming the list is a)

"+" connects two lists
a* integer repeats the list integer times
a[2] refers to the elements in the list
len(a) the number of elements in the list
a[1:3] slices, and the subsequence of the list includes 1 , Excluding 3
for value in a to loop through the list
expression in a to determine whether expression is in the list

List related methods

a.append(x) 把x加到a的最后
a.sort() 将a中元素排列.大小排序
a.sort(key=str) 按所有元素转化成字符串后的大小进行排序,即字典序排列
a.reverse() a中元素反转
a.index(x) 返回第一次出现x的索引值
a.insert(i,x) 在i位置插入x
a.count(x) 求x数量
a.remove(x) 删除第一次出现的元素x,无返回值
a.pop(i) 取i位置元素并删除它,不带参数,删除最后一个元素
a.expand([6,7]) 在原来列表上追加新列表
a.clear() 清除列表
a.copy() 生成一个新列表,复制a中的值
sorted(a)可以对列表进行排序

Insert picture description here

Create list

list((3,5,7,9,11)) 将元组转换为列表
list(range(1,10,2))range对象转换为列表
list('he llo') 将字符串转换为列表,每个字符转换为列表中的一个元素 空格标点符号都会转化为一个元素
list({
    
    3,5,7}) 集合转为列表 结果为[3,5,7]
x=list() 空列表
del x 删除列表对象,删除之后无法访问

Use the index to access the list, be careful not to cross the boundary

print(a)打印a中所有元素
print(a[-1])打印列表中最后一个元素

List comprehension

data = [2**i for i in range(64) ]
等价于 
data=[]
for i in range(64):
	data.append(2**i)

I.e. a list from 2 0 to 2 63

data = [num for num in range(20) if num%2==1]
等价于
data=[]
for num in range(20):
	if num%2==1:
		data.append(num)

Odd column list

Tuple

basic concepts

Tuples are types that contain multiple elements. The elements are separated by commas .
For example: t=(123,4,"h")
You can create a tuple by putting several elements in a pair of parentheses. If there is only one element, you need to add an extra comma .
(3,)
You can also use the tuple() function to convert lists, dictionaries, collections, strings, and range objects, map objects, zip objects or other similar objects into tuples.
Tuples can be empty. t2=()
A tuple can also be used as an element of another tuple. At this time, the tuple as an element needs to add parentheses. So as to avoid ambiguity.
t=(123,4,("hl",5))
each element in the tuple has a sequence relationship. The elements in the tuple can be accessed by index.
The T[0]
tuple cannot be changed or deleted after it is defined.
Similar to the string type, some elements in the tuple can be accessed through the index area.
t3[1:]
is the same as a string, plus and multiplication can be used between tuples.
Insert picture description here
Here 1, 2 is a tuple

The difference between tuples and lists

Tuples are immutable, and you cannot directly modify the value of the elements in the tuple. Nor can you add or delete elements to a tuple. Therefore, the tuple does not provide append(), expand(), insert(), etc. methods, nor remove() and pop() methods.
The access speed of tuples is faster than the list, and the overhead is smaller. If a series of constant values ​​are defined, the main purpose is to traverse them or other similar operations. It is generally recommended to use tuples instead of lists.
Tuples can make the code more secure. For example, using tuples to pass parameters when calling a function can prevent the elements from being modified in the function, while using a list cannot guarantee this.
Tuples can be used as the keys of a dictionary, and can also be used as elements of a set, but not a list, nor can a tuple containing a list.

Generator expression

gen =(2**i for i in range(8))  创建生成器对象。生成二的零次方,二的一次方,一直到二的七次方。
print(gen)
print(list(gen))转换为列表。用完了生成器对象中的所有元素。
print(tuple(gen))转换为元组得到空元组。
#gen作为List函数的参数传入使用,使用之后,他就为空了。这个生成器对象就已经为空,所以生成的元组为空元组。

The element in the generator expression becomes invalid after being used once. Can only be used once.

Slice operation

Applies to lists and tuples.
Slicing is a syntax used to obtain some elements in an ordered sequence such as list tuples and strings. Formally, slicing is done using three numbers separated by two colons.
[start:end:step]Among them, start defaults to zero.
Step is one by default. When step is a negative integer, it means reverse slice. At this point start should be to the right of end.
end means not including end.
When there is only one colon, a copy of all elements is returned. Example: a[:]
a[3:] All elements after subscript 3, which does not include the element of subscript 3.
a[-3:] The last 3 elements
a[:-5] All elements except the last five elements.
a[: :3] Choose one for every three elements.

Application of tuples

If you do not want the data to be modified, write it as a tuple type

Lists and tuples, in fact, are the derived form of sequence, here is a summary

Insert picture description here

Guess you like

Origin blog.csdn.net/bj_zhb/article/details/104626481