python basis filter, lists, dictionaries, filter the data according to the conditions set

from random import randint

data = [randint(-10, 10) for _ in xrange(10)]
print data
e = filter(lambda x: x >= 0, data)
print e

 

 


Or using a list comprehension speed

[x for x in data if x >= 0]

 


Screening dictionary

d = {x: randint(60, 100) for x in xrange(1, 21)}
print d
print {k: v for k, v in d.iteritems() if v > 90}

Screening collection 

Find divisible by 3

data = [randint(-10, 10) for _ in xrange(10)]
s = set(data)
print s
print {x for x in s if x % 3 == 0}

 

Guess you like

Origin www.cnblogs.com/angdh/p/11267298.html