Suppose I have a set of tuples with people's names. I want to find everyone who shares the same last name, excluding people who don't share their last name with anyone else:
# input
names = set([('John', 'Lee'), ('Mary', 'Miller'), ('Paul', 'Ryan'),
('Bob', 'Ryan'), ('Tina', 'Lee'), ('Bob', 'Smith')])
# expected output
{'Lee': ['Tina', 'John'], 'Ryan': ['Bob', 'Paul']} # or similar
This is what I am using
def find_family(names):
result = {}
try:
while True:
name = names.pop()
if name[1] in result:
result[name[1]].append(name[0])
else:
result[name[1]] = [name[0]]
except KeyError:
pass
return dict(filter(lambda x: len(x[1]) > 1, result.items()))
This looks ugly and inefficient. Is there a better way?
Instead of using a while loop, use a for loop (or similar construct) over the set contents (and while you're at it, you can destructure the tuples):
You might want to use a
defaultdict
orOrderedDict
(http://docs.python.org/library/collections.html) to hold your data in the body of the loop.You can get a dictionary of all the people where the keys are their lastnames easily with a for-loop:
Then, you just need to filter the dictionary with the condition
len(names) > 1
. This filtering could be done using a "dictionary comprehension":defaultdict
can be used to simplify the code:This prints: