To obtain a list of all the odd and construct a new list

Original link: http://www.cnblogs.com/funny2008/p/11088872.html

1.filter method of obtaining a list of all the odd -1 and configured new list:

a = [1,2,3,4,5,6,7,8,9,10]

def func(n):

        return n%2 ==1

list1 =list(filter(func,a))

print ( 'filter odd result table:', list1)

 

2.filter method of obtaining a list of all of the odd and -2 constructs a new list:

x = [1,2,3,4,5,6,7,8,9,10]

list2 =filter(lambda x:x%2==1,x)

print ( 'filter odd results in Table 2:', list (list2))

Derivation of formula 3 lists all the odd list of requirements and configured new list:

a = [1,2,3,4,5,6,7,8,9,10]

a = [ifor iin aif i% 2! = 0] # uneven number

print ( "filter odd results in Table", a)

You like it more than what method?

Reproduced in: https: //www.cnblogs.com/funny2008/p/11088872.html

Guess you like

Origin blog.csdn.net/weixin_30835933/article/details/94785259