Generating a list of EVEN numbers in Python

前端 未结 14 622
轻奢々
轻奢々 2021-01-17 15:48

Basically I need help in generating even numbers from a list that I have created in Python:

[1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597         


        
14条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-17 16:24

    You could do this using the filter function as follows:

    F = [1, 2]
    while F[-1] < 4000000:
        F.append(F[-1] + F[-2])
    print(F)
    print('\n')
    #create the variable that could store the sorted values from the list you have created.
    sorted_number=list(filter(lambda x:x%2==0,F))
    print(sorted_number)
    

提交回复
热议问题