pythonのfor文とリストリストリストの使い方、および深いコピーと浅いコピー
6731 ワード
python
1.for :
:
()
:
for in :
1
else:( )
2
:
:
s="ABCD" #
for ch in s:
print("ch--->>",ch)
:
ch--->> A
ch--->> B
ch--->> C
ch--->> D
for :
else
break ,else
2、range :
range(stop) 0~stop , stop ( stop)
range(start,stop[,step]) start~stop , stop ( stop)
stop
:
>>>help(range)
:
( )
:
range for
:
range(4) # 0,1,2,3
range(3,6) # 3,4,5
range(1,10,2) # 1,3,5,7,9
range(5,0,-2) # 5,3,1
range(4,0) #
**for :
for x in range(4,0):
print(x)
print('x ',x)#
**for :
range
i=6
for x in range(1,i): #for x in range(1,6)
print('x=',x,'i=',i)
i-=1
for :
for x in "ABC":
for y in "123":
print(x+y)
3、continue :
:
(while,for) ,
continue ,
:
continue
:
1. while continue, while
2. for continue ,
,
:
continue
for x in range(10):
if x % 2 == 0:# ,
continue
print(x)
while continue
:
x=0
while x<5:
if x==2:
x+=1
continue
print(x)
x+=1
-------------------------------------------
:
:( )
while
for
break
continue
------------------------------------------
=========================================
、 list
:
,
1. :
,
,
python3 :
str
list
tuple
bytes
bytearray
2. :
[] #
L=[]
3. :
L=[1,2,3,4] #L
L=['Beijing','Shanghai','Shenzhen']
L=[1,'Two',3.14,' ']
L=[1,2,[3.1,3.2,3.2],4]
4. list
list() , []
list(iterable)
:
L = list() # L = []
L = list("hello") # L = ['h','e','l','l','o']
L = list(range(5)) #L = [0,1,2,3,4]
、 :
:
+ += * *=( )
+ ,
x=[1,2,3]
y=[4,5,6]
z=x+y #z=[1,2,3,4,5,6]
+=
:
+=
:
x=[1,2,3]
y=[4,5,6]
x +=y #x=[1,2,3,4,5,6]
x+="ABC" #x=[1,2,3,4,5,6,'A','B','C']
x += range(4,6) #x=[1,2,3,4,5]
# += ,
x = [1,2,3]
print(id(x))
x += [4,5,6]
print(id(x)) # +=
+= ,
sx+= "123" sx = sx +'123'
sx="ABC"
print(id(sx))
sx += '123'
print(id(sx))
*
x=[1,2]*3 # x=[1,2,1,2,1,2]
*= ,
x=[1,2,3]
x*=4 #x=[1,2,3,1,2,3,1,2,3,1,2,3]
、 :
:
< <= > >= == !=
:
[1,2,3] < [1,2,4] #True
[1,2,3] < [3,2,1] #True
[1,2,3] == [3,2,1] #False
[2,3] >= [1,2,3] #True
[1,"2"] > ['2',1] #
["ABC",1,2,3] < ["123",3,2] #True
L=[1,3,5,7]
for x in L:
print(x) #1 3 5 7
、 in /not in
, True, False
in
, True, False
:
x = [1 ,'Two', 3, ' ' ]
3 in x # True
'3' in x # False
10 in x #False
10 not in x #True
、 :
:
[ ]
:
( )
:
x=[1,3,5,7]
print(x[1]) #3
print(x[-1])#7
、 :
,
:
[ ]=
:
:
x=[1,2,3,4]
x[2]=3.14 #
、 :
[:]
[::]
,
:
x=[1,2,3,4,5,6,7,8]
x=[::2] #[1,3,5,7]
x=[1::3] #[2,5,8]
、 :
:
,
:
[ ]=
:
(=)
:
L =[2,3,4]
L[0:1] = [1.1,2.2] #L=[1.1,2.2,3,4]
L[2:]=[1.1,2.2,3.3,4.4,5.5] #L=[1.1,2.2,3.3,4.4,5.5]
L=[:]=[3,4] #L=[3,4]
L[0:0]=[1,2] #L=[1,2,3,4]
L[1:1=[3.14] #L=[3,3.14,4]
L[2:2]=[5,6] #L=[3,4,5,6]( )
L[::]=[] #
L[1:2]="ABC" #L=[2,'A','B','C',4]
L=[2,-1,7]
L[1:2]=range(3,7) #L=[2,3,4,5,6,7]
:
1 ,
:
L=[1,2,3,4,5,6,7,8]
L[1::2]="ABCD" # L=[]
L[1::2]="ABCDEF" #
、del
:
del [ ]
:del L[0]
del [ ]
:del L[1::2]
python3
len(s)
max(x)
min(x)
sum(x) ( )
any(x) , True
all(x) , True
:
L=[3,1,9,7,5]
print(len(L)) #5
print(max(L)) #9
print(min(L)) #1
print(sum(L)) #25
: sum
max
python3 (method)
:>>>help(list)
L.pop(index) del L[index]
L.index(v [, begin[, end]]) , begin ,
end , value ValueError
、 :
1. , ,
2. ,
3. ,
S.split(sep=None) , sep S ,
, ,
S.join(iterable)
, S
、 list comprehension
:
:
[ for in ]
[ for in if ]
:
# 1~9
#[1,4,9,16,...81]
:
L = []
for x in range(1,10):
L.append(x**2)
print(L)
:
L=[i**2 for i in range(1,10)]
print(L)
:
1~100
L = [i for i in range(1,100,2)]
print(L)
[i for i in range(1,100) if i%2==1]
、 :
:
[ for 1 in 1 if 1
for 2 in 2 if 2...]
:
# [10,20,30] [1,2,3]
,
L =[ x + y
for x in [10,20,30]
for y in [1,2,3]]
print(L) #[11,12,13,21,22,23,31,32,33]
--------------------------------------------------------------
shallow copy
, ,
:
L=[3.1,3.2]
L1=[1,2,L]
L2=L1.copy() #
print(L1)
print(L2)
L2[2][0]=3.14
print(L1) #[1,2,[3.14,3.2]]
print(L2) #[1,2,[3.14,3.2]]
L2=L1 # ,
deep copy
:
import copy #
L=[3.1,3.2]
L1=[1,2,L]
L2=copy.deepcopy(L) #
print(L1) #[1,2,[3.1,3.2]]
print(L2) #[1,2,[3.1,3.2]]
L2[2][0]=3.14
print(L1) #[1,2,[3.1,3.2]] <<