Python basic data types _ONLINE problem set _03

3.1 tuple (2,3) and the set { "four", 5,6} a list of synthetic

1 tuple,set,list = (1,2,3),{"four",5,6},[]
2 for i in tuple:
3     list.append(i)
4 for j in set:
5     list.append(j)
6 print(list)

3.2 List [3,7,0,5,1,8] is greater than the element 5 is set to 0, is less than 5 element is set to 1

1 list2 = [3,7,0,5,1,8]
2 print(list2)
3 for i in range(0,len(list2)):
4     if list2[i] >5:
5         list2[i] = 0
6     elif list2[i]<5:
7         list2[i]=1
8 print(list2)

3.3 List [ "mo", "deng", "ge"] and [2,3] is converted to [( "mo", 1), ( "deng", 2), ( "ge", 3) ]

1  # Method a: traversing element method 
2 Sl1, Nl1, new_list1 = [ " Mo " , " Deng " , " GE " ], [l, 2,3 ], []
 . 3  for I in Sl1:
 . 4      for J in Nl1 :
 . 5          IF Sl1.index (I) == Nl1.index (J):
 . 6              new_list1.append ((I, J))
 . 7  Print ( " new_list1 = " , new_list1)
 . 8  
. 9  # standard method the traversal: method two 
10 Sl2, Nl2, new_list2 = ["mo","deng","ge"],[1,2,3],[]
11 for a in range(0,len(Sl2)):
12     for b in range(0,len(Nl2)):
13         if a == b:
14             new_list2.append((Sl2[a],Nl2[b]))
15 print("new_list2=",new_list2)
16 
17 #方法三:切片组合法
18 Sl3,Nl3=["mo","deng", " GE " ], [l, 2,3 ]
 . 19  Print ( " new_list3 = " , [(Sl3 [0], Nl3 [0]), (Sl3 [. 1], Nl3 [. 1]), (Sl3 [2 ], Nl3 [2 ])])
 20 is  
21 is  # four: the subscript opportunistic traversal method 
22 is SL4, Nl4, new_list4 = [ " Mo " , " Deng " , " GE " ], [l, 2,3 ], [ ]
 23 is  for K in Range (0,3 ):
 24      new_list4 + = [(SL4 [K], Nl4 [K])]
 25  Print ( " new_list4 =" , New_list4) 
26 is # run results: 27 " "" 28 new_list1 = [( 'Mo',. 1), ( 'Deng', 2), ( 'GE',. 3)] 29 new_list2 = [( 'Mo', . 1), ( 'Deng', 2), ( 'GE',. 3)] 30 new_list3 = [( 'Mo',. 1), ( 'Deng', 2), ( 'GE',. 3)] 31 is new_list4 = [( 'Mo',. 1), ( 'Deng', 2), ( 'GE',. 3)] 32 "" "

3.4 If a = dict (), so that b = a, performing b.update ({ "x": 1}), a change also, why, how to avoid

Reason: a variable to another variable is equivalent to the value of two variables refer to the same stored address

Solution: re-open space can cancel the association between two variables (what each expression will have value will re-open space, the value depends on the variable name referenced is assigned to it)

1  # Method a: copy () function copies 
2 A = {1: " Mo " , 2: " Deng " }
 . 3 B = a.copy ()
 . 4 b.update ({ " X " : " / " })
 . 5  Print (A, B)
 . 6  
. 7  # method two: unpacking assignment method 
. 8 A = {. 1: " Mo " , 2: " Deng " }
 . 9 B = dict ()
 10  b.update (A)
 . 11 b.update ( { " the X-":"/"})
12 print(a,b)
13 
14 #运行结果:
15 """
16 {1: 'mo', 2: 'deng'} {1: 'mo', 2: 'deng', 'x': '/'}
17 {1: 'mo', 2: 'deng'} {1: 'mo', 2: 'deng', 'x': '/'}
18 """

3.5 two-dimensional structure of [[ 'a', 1], [ 'b', 2]], and (( 'x', 3), ( 'y', 4)) is converted into the dictionary

1  # two-dimensional structure of [[ "a", "/ "], [ "b", 2]] , and (( "x", 3) , ( "y", 4)) is converted into the dictionary 
2 List1, tuple1 = [[ " A " , " / " ], [ " B " , 2]], (( " X " ,. 3), ( " Y " ,. 4 ))
 . 3 dict1 = dict (List1)
 . 4 dict2 = dict ( tuple1)
 . 5  Print (dict1, dict2)
 . 6  # run results: 
. 7  "" " 
. 8  { 'A': '/', 'B':} {2 'X':. 3, 'Y':
4}
9 """

3.6

3.7

 

 

Guess you like

Origin www.cnblogs.com/liulian999/p/12130967.html