-1

I have a list of lists

distances = [[female, 1],
[female, 2],  
[male, 3],
[male, 4],
[female, 5]] 

How can I get the mode(max occurrence among the elements) of the first column upto first k lists.
like, if k = 3, it should return 'female' as female occurs twice while male occurs once

Julkar9
  • 1,508
  • 1
  • 12
  • 24
  • 1
    what do you mean by *mode of the first column*? Do you mean the max count for first `k` rows? your output for `k=3` is `female` because it appears twice while `male` only once? be more specific – Tomerikoo Aug 22 '19 at 16:10
  • @Tomerikoo Yes the element with the highest frequency – Julkar9 Aug 22 '19 at 16:13
  • Possible duplicate of [Python-Counting element frequency in a 2D list](https://stackoverflow.com/questions/40140067/python-counting-element-frequency-in-a-2d-list) – Tomerikoo Aug 22 '19 at 16:17
  • @Tomerikoo The link doesn't answer the question completely, I am not looking for all the frequency counts just the mode of a specific column. Also the answers there are for all the elements not just a single column. – Julkar9 Aug 22 '19 at 16:24

3 Answers3

3

You can use the builtin statistics module for its mode function, and operator.itemgetter will allow easy access to just the first element in each list.

from statistics import mode
from operator import itemgetter

distances = [['female', 1], ['female', 2], ['male', 3], ['male', 4], ['female', 5]]
k = 3

print(mode(map(itemgetter(0), distances[:k])))
#'female'

You can also use collections.Counter to get counts of each sex.

Counter(map(itemgetter(0), distances[:k])))
#Counter({'female': 3, 'male': 2})

And you can use max on that too and come up with the same result:

sexes = Counter(map(itemgetter(0), distances[:k]))
print(max(sexes, key=sexes.get))
#'female'
Jab
  • 26,853
  • 21
  • 75
  • 114
0

You can try something like:

distances = [['female', 1],
['female', 2],  
['male', 3],
['male', 4],
['female', 5]]

def get_mode(k_max):
    l = [d[0] for d in distances[:k_max]] # make list of elements up to given k_max
    return max(set(l), key=l.count) # return most frequent element

get_mode(3)
'female'

This assumes that the list of lists is ordered, as in your example.

jamesoh
  • 372
  • 1
  • 10
0

This should work,

import statistics as stats

female = 'f'
male = 'm'

distances = [[female, 1],
[female, 2],  
[male, 3],
[male, 4],
[female, 5]] 
distmode = []
k = 0
for i in distances:
    distmode.append(i[0]) #take first entry in each list
    k += 1
    if k >= 3: #break at k=3
        break
print(stats.mode(distmode))
Anadactothe
  • 170
  • 1
  • 9