Suppose I have a list of Person objects, which have an age and room_number attribute, and I have written a check() function, which returns True, if person.age() and person.room_number() are satisfactory, False otherwise.
filter(check, list_of_people_objects)
would return a list of Person objects which satisfies the criteria of check()
However, my question is, is there a way of returning a list of each approved Person's room number without iterating through the list twice, like so without using list comprehension? So filtering, but returning a more specific attribute of the iterable.
map(lambda x: x.room_number(), filter(check, list_of_people_objects))
There are in fact two ways.
itertools
map(..., itertools.ifilter(..))
List comprehension
[x.room_number() for x in people if check(x)]
Which you choose is mostly a matter of taste, but convention leans towards the latter.
In the case of object filtering where you want to do an inclusive union of a subset of properties to equal a limited set of values and then perform any option (including list the attribute values) of the filtered list you can do the following using generators in a single statement (last line of code, the rest is there for instruction showing generating a large list of objects using matrix multiplication to generate constructor params)
#!/usr/bin/env python
import itertools
import pprint
class myObj(object):
attr_1 = None
attr_2 = None
attr_3 = None
def __init__(self, at1, at2, at3):
self.attr_1 = at1
self.attr_2 = at2
self.attr_3 = at3
super(myObj, self).__init__()
def __repr__(self):
return '<%s %s>' % (type(self), pprint.pformat(self.__dict__))
objs = itertools.starmap(myObj, itertools.product(iter('val_%d' % (i) for i in
range(1,4)), repeat=3))
filter_dict = {
'attr_1' : 'val_1',
'attr_2' : 'val_2',
'attr_3' : 'val_3',
}
print(list(result.attr_3 for result in objs if not list(False for pn,cval in
filter_dict.items() if getattr(result, pn, None) != cval)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With