Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- foo = [x for x in bar if x.occupants > 1]
- >>> numbers = [12, 34, 1, 4, 4, 67, 37, 9, 0, 81]
- result = []
- for index in range(len(numbers)):
- if numbers[index] > 5:
- result.append(numbers[index])
- print result #Prints [12, 34, 67, 37, 9, 81]
- result = []
- for number in numbers:
- if number > 5:
- result.append(number)
- print result #Prints [12, 34, 67, 37, 9, 81]
- result = [number for number in numbers if number > 5]
- [function(number) for number in numbers if condition(number)]
- result = filter(lambda x: x > 5, numbers)
- result = map(function, filter(condition, numbers)) #result is a list in Py2
- >>> class Bar(object):
- ... def __init__(self, occupants):
- ... self.occupants = occupants
- ...
- >>> bar=[Bar(0), Bar(1), Bar(2), Bar(3)]
- >>> foo = [x for x in bar if x.occupants > 1]
- >>> foo
- [<__main__.Bar object at 0xb748516c>, <__main__.Bar object at 0xb748518c>]
- >>> Bar.__repr__=lambda self:"Bar(occupants={0})".format(self.occupants)
- >>> foo
- [Bar(occupants=2), Bar(occupants=3)]
Add Comment
Please, Sign In to add comment