Issue
I have a large list of tuples like [('a','b'), ('a','c'), ('b','e'), ('a','d')]
and I would like to find top N popular entries in this list. The most popular is the one which is repeated the most. Here top two popular are a
and b
. How can I utlize networkx
to solve this problem if the number of list items are of million size?
import networkx as nx
lists = [('a','b'), ('a','c'), ('b','e'), ('a','d')]
G=nx.Graph()
G.add_edges_from(pair_names)
nx.degree(G)
this gives me list of popularity but I am having trouble displaying the top N popular items.
Solution
Any particular reason why you would want to use networkx
?
You can achieve this simply with collections.Counter
and itertools.chain
:
from collections import Counter
from itertools import chain
l = [('a','b'), ('a','c'), ('b','e'), ('a','d')]
Counter(chain.from_iterable(l)).most_common(2)
NB. Here for the top 2
Output: [('a', 3), ('b', 2)]
To only get the keys in decreasing order of frequency:
c = Counter(chain.from_iterable(l))
list(dict(c.most_common(2)))
Output: ['a', 'b']
Answered By - mozway Answer Checked By - Terry (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.