You can simply use collections.Counteron csv.reader:
from collections import Counter
>>> cnt = Counter(frozenset(item.strip() for item in line if item.strip()) for line in csv.reader(f))
() .most_common Counter:
>>> cnt.most_common()
[(frozenset({'Aaron Ekblad', 'Denis Malgin', 'Mike Matheson'}), 2),
(frozenset({'Aaron Ekblad', 'Jonathan Huberdeau', 'Keith Yandle'}), 2),
(frozenset({'Adam Henrique', 'Brandon Montour', 'Corey Perry'}), 2),
(frozenset({'Adam Henrique'}), 2),
(frozenset({'Aaron Ekblad', 'Keith Yandle', 'Vincent Trocheck'}), 1),
(frozenset({'Aaron Ekblad', 'Jamie McGinn', 'Keith Yandle'}), 1),
(frozenset({'Aaron Ekblad', 'Aleksander Barkov', 'Jonathan Huberdeau'}), 1),
(frozenset({'Aaron Ekblad'}), 1),
(frozenset({'Adam Erne', 'Andrej Sustr', 'Vladislav Namestnikov'}), 1),
(frozenset({'Adam Erne', 'Anthony Cirelli'}), 1),
(frozenset({'Adam Erne'}), 1),
(frozenset({'Adam Henrique', 'Rickard Rakell', 'Ryan Getzlaf'}), 1),
(frozenset({'Adam Henrique', 'Brandon Montour', 'Ryan Getzlaf'}), 1),
(frozenset({'Adam Henrique', 'Andy Greene', 'Brian Gibbons'}), 1),
(frozenset({'Adam Henrique', 'Ryan Getzlaf'}), 1),
(frozenset({'Adam Henrique', 'Ondrej Kase'}), 1),
(frozenset({'Adam Henrique', 'Josh Manson'}), 1),
(frozenset({'Adam Henrique', 'Brian Gibbons'}), 1)]
( ), :
>>> cnt.most_common(1)
[(frozenset({'Aaron Ekblad', 'Denis Malgin', 'Mike Matheson'}), 2)]
, , , , max:
>>> maximum_occurences = max(cnt.values())
>>> [group for group, occurences in cnt.items() if occurences == maximum_occurences]
[frozenset({'Aaron Ekblad', 'Denis Malgin', 'Mike Matheson'}),
frozenset({'Aaron Ekblad', 'Jonathan Huberdeau', 'Keith Yandle'}),
frozenset({'Adam Henrique', 'Brandon Montour', 'Corey Perry'}),
frozenset({'Adam Henrique'})]
:
Counter , , - . , csv.reader (, ), .
tuples , , , , , , . , frozenset. : , , , .
, csv, , , "" / ,
item.strip() for item in line if item.strip()
frozenset.
, strips , map:
frozenset(item for item in (item.strip() for item in line) if item)
frozenset(item for item in map(lambda x: x.strip(), line) if item)
frozenset(item for item in map(str.strip, line) if item)
, filter bool:
frozenset(filter(bool, map(str.strip, line)))
, " ". itertools.
: , Counter, most_common:
cnt = Counter({k: v for k, v in cnt.items() if len(k) > 1})
Counter, .
, "", , .
Counter(tuple(Counter(filter(bool, map(str.strip, line))).most_common()) for line in csv.reader(f))
, .