跳转至

python-study-list

inp = input("请输入你的名字")
1
请输入你的名字李思涵
print(inp)
1
2
3
a
fa
asfasf
1
李思涵
inp = input("数字")
1
数字3
print(inp)
1
3
print(inp+"无语")
1
3无语
inp = int(input("再来"))
print(inp+"无语")
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
再来6



---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

~\AppData\Local\Temp\ipykernel_13512\1933184884.py in <module>
      1 inp = int(input("再来"))
----> 2 print(inp+"无语")


TypeError: unsupported operand type(s) for +: 'int' and 'str'
print(inp+3)
1
9
list=[1,2,3,4,5,6]
list[0]
1
1
list[:]
1
[1, 2, 3, 4, 5, 6]
list[0:6:2]
1
[1, 3, 5]
list[0:6:-1]
1
[]
list[0:6:3]
1
[1, 4]
list[-1]
1
6
list[0:6:-2] 
1
[]
list[::-2]
1
[6, 4, 2]

增删改查

append在末尾添加一个新元素

heros=['钢铁侠','绿巨人']
heros.append('黑寡妇')
heros
1
['钢铁侠', '绿巨人', '黑寡妇']

extend在末尾添加几个元素

heros.extend(["鹰眼","灭霸","雷神"])
heros
1
['钢铁侠', '绿巨人', '黑寡妇', '鹰眼', '灭霸', '雷神']

可以使用:操作(切片)来代替

s=[1,2,3,4,5]
s[len(s):]=[6]
s
1
[1, 2, 3, 4, 5, 6]
s[len(s):]=[7,8,9]
s
1
[1, 2, 3, 4, 5, 6, 7, 8, 9]

insert任意位置插入元素

s=[1,3,4,5]
s.insert(1,2)
s
1
[1, 2, 3, 4, 5]
s.insert(len(s),6)
s
1
[1, 2, 3, 4, 5, 6]

remove删除指定元素(内容)

heros.remove("灭霸")
heros
1
['钢铁侠', '绿巨人', '黑寡妇', '鹰眼', '雷神']
heros.remove("金莲")
1
2
3
4
5
6
7
8
9
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

~\AppData\Local\Temp\ipykernel_4012\2787210069.py in <module>
----> 1 heros.remove("金莲")


ValueError: list.remove(x): x not in list

pop删除指定(位置)元素

heros.pop(2)
1
'黑寡妇'
heros
1
['钢铁侠', '绿巨人', '鹰眼', '雷神']
heros.clear()
heros
1
[]

替换实际上是两步操作先删除,再插入

heros=['蜘蛛侠', '绿巨人', '黑寡妇', '鹰眼', '灭霸', '雷神']
heros[4]="钢铁侠"
heros
1
['蜘蛛侠', '绿巨人', '黑寡妇', '鹰眼', '钢铁侠', '雷神']
heros[3:]=["武松","林冲","李逵"]
heros
1
['蜘蛛侠', '绿巨人', '黑寡妇', '武松', '林冲', '李逵']

sort排序

nums=[3,1,9,6,8,3,5,3]
nums.sort()
nums
1
[1, 3, 3, 3, 5, 6, 8, 9]

reverse排序

nums.reverse()
nums
1
[9, 8, 6, 5, 3, 3, 3, 1]
heros.reverse()
heros
1
['李逵', '林冲', '武松', '黑寡妇', '绿巨人', '蜘蛛侠']

参数设置同样效果

nums=[3,1,9,6,8,3,5,3]
nums.sort(reverse=True)
nums
1
[9, 8, 6, 5, 3, 3, 3, 1]

count查找某个元素出现的次数

nums.count(3)
1
3

index获取索引值(第一次出现)

heros.index("绿巨人")
1
4
heros[heros.index("绿巨人")]="神奇女侠"
heros
1
['李逵', '林冲', '武松', '黑寡妇', '神奇女侠', '蜘蛛侠']
nums=[3,1,9,6,8,3,5,3]
nums.index(3)
1
0

index(x,start,end)

nums.index(3,1,7)
1
5

copy拷贝

shallow copy

nums_copy1=nums.copy()
nums_copy1
1
[3, 1, 9, 6, 8, 3, 5, 3]
nums_copy2=nums
nums_copy2
1
[3, 1, 9, 6, 8, 3, 5, 3]
nums_copy3=nums[:]
nums_copy3
1
[3, 1, 9, 6, 8, 3, 5, 3]

list的* +

s=[1,2,3]
t=[4,5,6]
s+t
1
[1, 2, 3, 4, 5, 6]
s*3
1
[1, 2, 3, 1, 2, 3, 1, 2, 3]

嵌套列表

matrix=[[1,2,3],[4,5,6],[7,8,9]]
1
2
3
matrix=[[1,2,3],
        [4,5,6],
        [7,8,9]]
matrix
1
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

访问嵌套列表

1
2
3
for i in matrix:
    for each in i:
        print(each)
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9

换行

1
2
3
4
for i in matrix:
    for each in i:
        print(each,end=' ')
    print()
1
2
3
1 2 3 
4 5 6 
7 8 9
matrix[0]
1
[1, 2, 3]
matrix[0][0]
1
1
matrix[1][1]
1
5
matrix[2][2]
1
9
A = [0]*3
A
1
[0, 0, 0]
for i in range(3):#从0到2
    A[i]=[0]*3
A
1
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]

错误写法-虽然结果相同

B=[[0]*3]*3
B 
1
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
A[1][1]=1
A
1
[[0, 0, 0], [0, 1, 0], [0, 0, 0]]
B[1][1]=1
B
1
[[0, 1, 0], [0, 1, 0], [0, 1, 0]]

每个嵌套中的元素都被改变-探究(is)

x = "FishC"
y = "FishC"
x is y
1
True
 x=[1,2,3]
y=[1,2,2]
x is y
1
False
可知同个位置存放,而list会开两个内存
A[0] is A[1]
1
False
A[1] is A[2]
1
False
B[0] is B[1]
1
True
B[1] is B[2]
1
True

A每个元素单独开辟一个空间

B每个元素始终指向同一个空间

变量不是盒子

x=[1,2,3]
y=x

变量的赋值与拷贝

赋值不会创建新空间,是引用,拷贝则是创建新空间。不互相影响

浅拷贝和深拷贝

x = [1,2,3]
y=x.copy()
y
1
[1, 2, 3]
x[1]=1
x
1
[1, 1, 3]
y
1
[1, 2, 3]
x=[1,2,3]
y=x[:]
x[1]=1
x
1
[1, 1, 3]
y
1
[1, 2, 3]

二维开始有区别

x=[[1,2,3],[4,5,6],[7,8,9]]
y=x.copy()
x[1][1]=0
x
1
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
y
1
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]

引入深拷贝 copy.deepcopy(x)

import copy
x=[[1,2,3],[4,5,6],[7,8,9]]
y=copy.copy(x)
x[1][1]=0
x
1
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
y
1
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
x=[[1,2,3],[4,5,6],[7,8,9]]
y=copy.deepcopy(x)
x[1][1]=0
x
1
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
y
1
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

列表推导式 [expression for target in iterable]

list_1=[1,2,3]
list_1*3
1
[1, 2, 3, 1, 2, 3, 1, 2, 3]
list
1
list
list_1=[1,2,3]
for i in range(len(list_1)):
    list_1[i]=list_1[i]*3
list_1
1
[3, 6, 9]
oho=[1,2,3,4,5]
oho=[i * 2 for i in oho]
oho
1
[2, 4, 6, 8, 10]

不仅仅少写代码,speed will be faster

使用C,而python使用虚拟机pvm

循环是迭代替换

推导式是直接创建

x = [i for i in range(10)]
x
1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
x = [i + 1for i in range(10)]
x
1
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
x = []
for i in range(10):
    x.append(i+1)
x
1
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [c * 2 for c in "FishC"]
y
1
['FF', 'ii', 'ss', 'hh', 'CC']

ord将字符转化为Unicode编码

code = [ord(c) for c in "FishC"]
code
1
[70, 105, 115, 104, 67]
matrix=[[1,2,3],[4,5,6],[7,8,9]]
col2=[row[1] for row in matrix]
col2
1
[2, 5, 8]
diag = [matrix[i][i] for i in range(len(matrix))]
diag
1
[1, 5, 9]
diag=[]
A=[0]*3
A
1
[0, 0, 0]
A=[[0]*3 for i in A]
A=[0]*3
A=[[0]*3 for i in range(3)]
A
1
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
A=[[0]*3 for i in A]
A
1
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
A[1][1]=1
A
1
[[0, 0, 0], [0, 1, 0], [0, 0, 0]]

列表推导式 [expression for target in iterable if condition]

[i for i in range(10) if i % 2 == 0]
1
[0, 2, 4, 6, 8]
enve=[i for i in range(10) if i % 2 == 0]
enve
1
[0, 2, 4, 6, 8]
enve=[i + 1 for i in range(10) if i % 2 == 0]
enve
1
[1, 3, 5, 7, 9]

先执行if语句再执行左侧for语句

words=["Great","FishC","Brilliant","Excellent","Fantistic"]
fwords=[w for w in words if w[0] == 'F']
fwords
1
['FishC', 'Fantistic']

列表推导式 [expression for target in iterable if condition

1
2
3
4
                      for target in iterable2 if condition2
                      for target in iterable3 if condition3
                      --
                      for target in iterableN if conditionN]嵌套循环
matrix=[[1,2,3],[4,5,6],[7,8,9]]#内存上横向存储
flatten=[col for row in matrix for col in row]
flatten
1
[1, 2, 3, 4, 5, 6, 7, 8, 9]
flatten = []
1
2
3
for row in matrix:
    for col in row:
        flatten.append(col)
flatten
1
[1, 2, 3, 4, 5, 6, 7, 8, 9]

笛卡尔积

[x + y for x in "fishc" for y in "FISHC" ]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
['fF',
 'fI',
 'fS',
 'fH',
 'fC',
 'iF',
 'iI',
 'iS',
 'iH',
 'iC',
 'sF',
 'sI',
 'sS',
 'sH',
 'sC',
 'hF',
 'hI',
 'hS',
 'hH',
 'hC',
 'cF',
 'cI',
 'cS',
 'cH',
 'cC']

临时变量 _

_ = []
1
2
3
for x in "fishc":
    for y in "FISHC":
        _.append(x+y)
_
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
['fF',
 'fI',
 'fS',
 'fH',
 'fC',
 'iF',
 'iI',
 'iS',
 'iH',
 'iC',
 'sF',
 'sI',
 'sS',
 'sH',
 'sC',
 'hF',
 'hI',
 'hS',
 'hH',
 'hC',
 'cF',
 'cI',
 'cS',
 'cH',
 'cC']
[[x,y] for x in range(10) if x % 2 == 0 for y in range(10) if y % 3 == 0]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
[[0, 0],
 [0, 3],
 [0, 6],
 [0, 9],
 [2, 0],
 [2, 3],
 [2, 6],
 [2, 9],
 [4, 0],
 [4, 3],
 [4, 6],
 [4, 9],
 [6, 0],
 [6, 3],
 [6, 6],
 [6, 9],
 [8, 0],
 [8, 3],
 [8, 6],
 [8, 9]]
_ = [];
1
2
3
4
5
for x in range(10):
    if x % 2 == 0:
        for y in range(10):
            if y % 3 == 0:
                _.append([x,y])
_
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
[[0, 0],
 [0, 3],
 [0, 6],
 [0, 9],
 [2, 0],
 [2, 3],
 [2, 6],
 [2, 9],
 [4, 0],
 [4, 3],
 [4, 6],
 [4, 9],
 [6, 0],
 [6, 3],
 [6, 6],
 [6, 9],
 [8, 0],
 [8, 3],
 [8, 6],
 [8, 9]]

KISS原则:Keep It Simple & Stupid