Basic Exercise II:
1, some numbers exist in the list, such as: L = [1, 3, 2, 1, 6, 4, 2, ....., 98, 82]
1) Save the number appearing in list L in another list L2
Requirements:
Repeated occurrences of a number only keep one copy in the L2 list (deduplication)
2) Save the number that appears in the list two times in the L3 list (keep one copy in the L3 list)
L = [1, 3, 2, 1, 6, 4, 2, 98, 82] #1) Save the number appearing in list L in another list L2#Requirements:#repeated occurrences of a number only keep one copy in the L2 list (deduplication)L2 = []#prepare to put data that is not duplicated forXinchL:ifX not inchL2:#there's no L2.l2.append (x)Print("l2=", L2)#2) Save the number that appears in the list two times in the L3 list (keep one copy in the L3 list)L3 = [] forXinchL:ifX not inchL3 andL.count (x) = = 2: L3.append (x)Print('l3=', L3)
2. Generate the first 40 Fibonacci numbers (Fibonacci)
1 1 2 3 5 8 13 21 ...
This number is required to be stored in the list L, and the number is printed at the end
(the first two of Fibonacci are 1, 1, and the number after is the first two numbers)
L = []#ready to put data#Method 1 (classic method)#A = 0#B = 1 # The number that is currently being calculated#While Len (L) < 40: # Not enough 40#L.append (b) # Add the number you've got to the list #figure out the next number, still in B, to prepare for the next cycle.#C = a + B # next time number#a = b # give the current number to a#B = C # and then give the number already counted to B.#Method 2#A = 0#B = 1 # The number that is currently being calculated#While Len (L) < 40: # Not enough 40#L.append (b) # Add the number you've got to the list #figure out the next number, still in B, to prepare for the next cycle.#A, B = B, a + b#Method 3 (only possible in Python, not in other languages)L = [1, 1] whileLen (L) < 40: L.append (l[-1] + l[-2])PrintL
Python (Basic Exercise II)