>>> from random import randint
>>> data = [randint(-10,10) for x in xrange(10)]
>>> data
[-1, 8, -9, 9, 10, -2, 6, -8, -2, 10]

>>> res = []
>>> for x in data:
>>> if x >= 0:
>>> res.apend(x) >>> res [8,9,10,6,10]

>>> filter(lambda x: x >= 0, data) [8,9,10,6,10]

>>> [x for x in data if x >= 0]
[8, 9, 10, 6, 10]

>>> students = {x: randint(30, 100) for x in xrange(1, 21)}
>>> students
{1: 31, 2: 37, 3: 34, 4: 77, 5: 69, 6: 68, 7: 84, 8: 83, 9: 49, 10: 60, 11: 39,
12: 96, 13: 79, 14: 80, 15: 90, 16: 75, 17: 38, 18: 61, 19: 97, 20: 66}

>>> {k:v for k,v in students.iteritems() if v >= 80}
{7: 84, 8: 83, 12: 96, 14: 80, 15: 90, 19: 97}

>>> data
[-1, 8, -9, 9, 10, -2, 6, -8, -2, 10]
>>> s = set(data)
>>> s
set([6, 8, 9, 10, -1, -9, -8, -2])
>>> {x for x in s if x % 3 == 0}
set([9, 6, -9])

tags: gt,data,students,列表,字典,filter,res,randint,集合

1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责；
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性，不作出任何保证或承若；
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。