Python (Basic Exercise II)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.