Generating a list of EVEN numbers in Python

前端 未结 14 617
轻奢々
轻奢々 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:17

    The following sample should solve your problem.

    Newlist = []
    for x in numList:
       if x % 2 == 0:
          print x          
          Newlist.append(x)
    
    0 讨论(0)
  • 2021-01-17 16:19
    a = range(0,1000)
    b = []
    for c in a:
        if c%2==0:
            b.append(c)
    print b
    
    0 讨论(0)
  • 2021-01-17 16:20

    You can use list comprehension to generate a new list that contains only the even members from your original list.

    data = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
    

    then:

    new_data = [i for i in data if not i%2]
    

    yields

    [2, 8, 34, 144]
    

    Or alternatively use a generator expression if you don't need all of the numbers at once:

    new_data = (i for i in data if not i%2)
    

    The values then would be availabe as needed, for instance if you used a for loop:

    e.g.,

    for val in new_data:
       print val
    

    The advantage of the generator expression is that the whole list is not generated and stored in memory at once, but values are generated as you need them which makes less demand on memory. There are other important differences you might want to read up on at some point if you are interested.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2021-01-17 16:25

    iterate through the list and use the modulo operator to check even

    for number in list:
        if (number % 2) == 0: 
            ##EVEN
    
    0 讨论(0)
  • 2021-01-17 16:28

    Here are some of the different ways to get even numbers:

    CASE 1 in this case, you have to provide a range

    lst = []
    for x in range(100):
        if x%2==0:
        lst.append(x)
    print(lst)
    

    CASE 2 this is a function and you have to pass a parameter to check if it is an even no or not def even(rangeno): for x in range(rangeno): if rangeno%2 == 0: return rangeno else: return 'No an Even No'

     even(2)
    

    CASE 3 checking the values in the range of 100 to get even numbers through function with list comprehension

    def even(no):
    return [x for x in range(no) if x%2==0]
    
    even(100)
    

    CASE 4 This case checks the values in list and prints even numbers through lambda function. and this case is suitable for the above problem

    lst = [2,3,5,6,7,345,67,4,6,8,9,43,6,78,45,45]
    no = list(filter(lambda x: (x % 2 == 0), lst))
    print(no)
    
    0 讨论(0)
提交回复
热议问题