python - count most frequent elements in list of same length

  • Last Update :
  • Techknowledgy :

Last Updated : 17 Jul, 2021,GATE CS 2021 Syllabus

1._
Input: [2, 1, 2, 2, 1, 3]
Output: 2

Input: ['Dog', 'Cat', 'Dog']
Output: Dog

2

2

[2, 1]

Suggestion : 2

zip the list of strings to "transpose" them to present columns in the same iterator, apply collections.Counter on them, and use most_common method, remove the unwanted data

data = ""
"0004000000350
0000090033313
0004000604363
040006203330 b
0004000300 a3a
0004000403833
00000300333 a9
0004000003 a30 ""
"

import collections

counts = [collections.Counter(x).most_common(1)[0][0]
   for x in zip( * data.splitlines())
]

this yields:

['0', '0', '0', '4', '0', '0', '0', '0', '0', '3', '3', '3', '3']

You start using zip in order to interleave the characters in each string that are in the same relative position. Then take the mode of each tuple using scipy.stats.mode , and join the resulting strings from the generator expression:

l = ['0004000000350', '0000090033313', '0004000604363', '040006203330b',
   '0004000300a3a', '0004000403833', '00000300333a9', '0004000003a30'
]

from scipy.stats
import mode
   ''.join(mode(i).mode[0]
      for i in list(zip( * l)))

Output

'0004000003333'

Without import I would do :

data = [
   "0004000000350",
   "0000090033313",
   "0004000604363",
   "040006203330b",
   "0004000300a3a",
   "0004000403833",
   "00000300333a9",
   "0004000003a30",
]

#
return the most common elemebt in an iterable
most_common = lambda ite: max(ite, key = ite.count)

# print the most_common in each columns
print(map(most_common, zip( * data)))

#['0', '0', '0', '4', '0', '0', '0', '0', '0', '3', '3', '3', '3']
from collections
import Counter
   ''.join(Counter(i).most_common(1)[0][0]
      for i in zip( * l))

Since no one has used pandas, so by using pandas you can achieve this easily and efficient

a = ""
"0004000000350
0000090033313
0004000604363
040006203330 b
0004000300 a3a
0004000403833
00000300333 a9
0004000003 a30 ""
"

import pandas as pd
df = pd.DataFrame([list(j) for j in a.strip().split('\n')])
result = df.mode().to_string(header = None, index = None)
print(result)

""
" output 
0 0 0 4 0 0 0 0 0 3 3 3 3
   ""
"

Suggestion : 3

I've been looking for answers to this for anycodings_python the last few hours, without finding the anycodings_python answer I was looking for, so I decided to anycodings_python ask here instead.,Edit: Thanks for the answers, gave me just anycodings_python what I was looking for! :),So, say I've got a list of data with the anycodings_python same length such as;,(join the characters to recreate a anycodings_python string if needed using "".join(counts))

So, say I've got a list of data with the anycodings_python same length such as;

0004000000350
0000090033313
0004000604363
040006203330 b
0004000300 a3a
0004000403833
00000300333 a9
0004000003 a30

An example output would be something like;

0 0 0 4 0 0 0 0 0 3 3 3 3

zip the list of strings to "transpose" anycodings_python them to present columns in the same anycodings_python iterator, apply collections.Counter on anycodings_python them, and use most_common method, remove anycodings_python the unwanted data

data = ""
"0004000000350
0000090033313
0004000604363
040006203330 b
0004000300 a3a
0004000403833
00000300333 a9
0004000003 a30 ""
"

import collections

counts = [collections.Counter(x).most_common(1)[0][0]
   for x in zip( * data.splitlines())
]

this yields:

['0', '0', '0', '4', '0', '0', '0', '0', '0', '3', '3', '3', '3']

You start using zip in order to anycodings_python interleave the characters in each string anycodings_python that are in the same relative position. anycodings_python Then take the mode of each tuple using anycodings_python scipy.stats.mode , and join the anycodings_python resulting strings from the generator anycodings_python expression:

l = ['0004000000350', '0000090033313', '0004000604363', '040006203330b',
   '0004000300a3a', '0004000403833', '00000300333a9', '0004000003a30'
]

from scipy.stats
import mode
   ''.join(mode(i).mode[0]
      for i in list(zip( * l)))

Output

'0004000003333'

Without import I would do :

data = [
   "0004000000350",
   "0000090033313",
   "0004000604363",
   "040006203330b",
   "0004000300a3a",
   "0004000403833",
   "00000300333a9",
   "0004000003a30",
]

#
return the most common elemebt in an iterable
most_common = lambda ite: max(ite, key = ite.count)

# print the most_common in each columns
print(map(most_common, zip( * data)))

#['0', '0', '0', '4', '0', '0', '0', '0', '0', '3', '3', '3', '3']
from collections
import Counter
   ''.join(Counter(i).most_common(1)[0][0]
      for i in zip( * l))

Since no one has used pandas, so by anycodings_python using pandas you can achieve this easily anycodings_python and efficient

a = ""
"0004000000350
0000090033313
0004000604363
040006203330 b
0004000300 a3a
0004000403833
00000300333 a9
0004000003 a30 ""
"

import pandas as pd
df = pd.DataFrame([list(j) for j in a.strip().split('\n')])
result = df.mode().to_string(header = None, index = None)
print(result)

""
" output 
0 0 0 4 0 0 0 0 0 3 3 3 3
   ""
"