List python basis 3--

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/EngineerHe/article/details/98875751

3 basis python

Here Insert Picture Description

type of data

List

list (list) python is the most frequently used data types, arrays usually do in other languages; the list is written between square brackets ([]), comma-separated list of elements; it can be done most collection class data structure implementation. It supports character, numeric, string or even contain a list (called nesting), summary list of features are as follows:

  • Use brackets
  • Use commas to separate each element
  • Elements in the list can be numbers, strings, lists, or so can be put to, any type of nesting
  • Memory list can be modified after creation
  • String, like support indexing; Support slices; support cycle;
  • Delete, and modify the list of supported indexing and slicing
  • List ( 'string') into a string list
  • The list is ordered, the elements can be modified
  • A variable tabular data type

The following methods can create a list, you can use the method to see if the type is a list type.

# 直接定义
l1 = ['this', 'is', 'a', 'list']
l2 = [1, 2, 3, 4, 5, 6]
# 使用range构造一个列表
l3 = range(6)
# 使用list方法转换为列表
l4 = list('list')
# 使用推导公式创建列表
l5 = [i for i in range(6)]
# 嵌套
l6 = [1, 'ss', [2,3,4],{'key':'value'},('tuple',)]

Access and update a list of values

The subscript index to access values ​​in the list, can also be used in the form of slices get multiple values, a list with other languages, the subscript index is zero-based.

# 单下标索引
>>> list1 = ['this', 'is', 'a', 'list']
>>> print(list1[1])
is
# 切片,注意切片索引的时候,可以取到左下标不能取到右下标,如下[1:3],不能取3,只能获得下标1、2的值
>>> print(list1[1:3])
['is', 'a']
# 支持负索引,-1表示列表的最末段的元素,依次类推
>>> print(list1[-2])
a

List is a type of a variable may be dynamically change the contents of the existing list and the list of the lengths of each index

# 修改某个索引下的值
>>> list2 = [1, 2,'a', 'b']
>>> list2[1] = 66
>>> print(list2)
[1, 66, 'a', 'b']
# append 添加元素,在现有列表的尾部添加
>>> list2.append(3)
>>> print(list2)
[1, 66, 'a', 'b', 3]
# extend 使用appen是添加某一个元素,要是另一个列表中的所有元素,可以使用extend
>>> list3 = [1,2,3]
>>> list2.extend(list3)
>>> print(list2)
[1, 2, 'a', 'b', 1, 2, 3]
# insert(i, value) 添加元素,在下标为i的地方插入value
>>> list2.insert(2, 'c')
>>> print(list2)
[1, 66, 'c', 'a', 'b', 3]
# pop删除元素,默认删除最有一个元素,并返回删除的值,还可指定下标删除,要是超出索引范围,则会抛出异常
>>> list2.pop()
3
>>> print(list2)
[1, 66, 'c', 'a', 'b']
>>> list2.pop(2)
'c'
>>> print(list2)
[1, 66, 'a', 'b']
# 使用remove移除列表中第一次出现的元素
>>> list2.remove('a')
>>> print(list2)
[1, 66, 'b']
# del 删除列表中的元素,无返回值
>>> del list2[0]
>>> print(list2)
[66, 'b']

Operator List Script: '+' and '*', where '+' is used in combination list, '*' list for repeatedly

# '+'拼接列表
>>> list3 = [1,2,3]
>>> list4 = [4,5,6]
>>> l = list3+list4
>>> print(l)
[1, 2, 3, 4, 5, 6]
# '*'重复列表中的元素
>>> l = list4 * 3
>>> print(l)
[4, 5, 6, 4, 5, 6, 4, 5, 6]
# in 判断元素是不是在列表中
>>> 6 in l
True
# 迭代, 遍历每一个元素
for i in list4:
	print(i, end='')
>>> 456

A list of commonly used functions

>>> list5 = [1,2,3,6,7,8]
# len 获取列表的长度
>>> print(len(list5))
6
# max 获取最大值,字符也适用,如果是一个字符串,从第一个开始比较
>>> print(max(list5))
8
# min获取最小值
>>> print(min(list5))
1
# list,可以把元组、字符串转换为列表
>>> list6 = list(s)
>>> print(list6)
['s', 't', 'r']
>>> num = (1,2,3)
>>> type(num)
<class 'tuple'>
>>> list7 = list(num)
>>> type(list7)
<class 'list'>

List method

He said earlier append, extend, insert, pop, remove

There are common count, index, sort, reverse, clear, copy

# count 统计列表中某个元素出现的次数
>>> list8 = [1,1,2,6,6,8,9]
>>> list8.count(1)
2

# index 获取某一个元素在列表中第一次出现的下标
>>> list8 = [1,1,2,6,6,8,9]
>>> list8.index(6)
3

# sort 对原列表进行排序,默认是升序排列,可以通过reverse选择升序或者降序
>>> list8.sort()
>>> list8
[1, 1, 2, 6, 6, 8, 9]
>>> list8.sort(reverse=True)
>>> list8
[9, 8, 6, 6, 2, 1, 1]

# clear 清空列表中的元素
>>> list8.clear()
>>> list8
[]
>>> print(len(list8))
0

There is also a copy method, where the need toNote theNote theNote thePit came

Look at an example

>>> a = [1,2,3]
>>> b = a
>>> b[0] = 66
>>> print(a)
[66, 2, 3]
>>> print(id(a))
1993459824968
>>> print(id(b))
1993459824968

In this example, the value of a given assignment b, we modify the first element of b, in theory, should not be a change, a result also changed; id can know by looking at these two variables, they are in the same address, but not the same name, this is why?

In python, we define a = [1,2,3], give in memory management [1,2,3]allocates memory addresses, and then marked with a label on this address, it can be said that a [1,2,3]this piece of memory alias, then we operate b=ajust the name for a moment, two of them a still point to the same address, so b change a consent for a change; in the following diagram to explain a:

Here Insert Picture Description

More professional, he said, python into deep and shallow copy copy, I directly put a figure drawn

Here Insert Picture Description

# python中的拷贝
# 浅拷贝
a = [1, 2, 3, [4,5,6]]
b = a.copy()
c = a[:]
>>> b[0] = 66
>>> c[0] = 666
>>> print(a)
[1, 2, 3, [4, 5, 6]]
>>> print(b)
[66, 2, 3, [4, 5, 6]]
>>> print(c)
[666, 2, 3, [4, 5, 6]]
>>> c[3][1] = 555
>>> a
[1, 2, 3, [4, 555, 6]]
>>> b
[66, 2, 3, [4, 555, 6]]
>>> c
[666, 2, 3, [4, 555, 6]]
>>>

Can be understood from the above source, may be shallow copy by copy method can also be performed using the method copy sections, but entire replication surface elements of such operations, the deep still not completely copy

import copy
>>> d = copy.deepcopy(a)
>>> d[1] = 0
>>> d[3][0] = 'xxx'
>>> priint(a)
>>> print(a)
[1, 2, 3, [4, 5, 6]]
>>> print(d)
[1, 0, 3, ['xxx', 5, 6]]

Shallow copy, you can also use the slice method copy, but copying the entire surface of the elements of such operations, the deep still not completely copy

import copy
>>> d = copy.deepcopy(a)
>>> d[1] = 0
>>> d[3][0] = 'xxx'
>>> priint(a)
>>> print(a)
[1, 2, 3, [4, 5, 6]]
>>> print(d)
[1, 0, 3, ['xxx', 5, 6]]

By introducing a copy packet, the method can be achieved using deepcopy deep copy

Guess you like

Origin blog.csdn.net/EngineerHe/article/details/98875751