PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, June 28, 2022

[FIXED] How to find the N most common occurring element in a list?

 June 28, 2022     graph, python     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing