I have a Python list of lists:
a = [[1,2,3], [1,3,3], [0,3,0]]
I want to take a mode of it, in this way:
mode of zeroth position of each list: 1, 1, 0
= 1
mode of first position of each list: 2, 3, 3
= 3
mode of second position of each list: 3, 3, 0
= 3
So, the output will be:
output = [1, 3, 3] # expected output
Can anyone tell me, how to take such mode?
I tried using statistics library: statistics.mode(a)
, but it is giving me this error: TypeError: unhashable type: 'list'
. Do you have any other method?
CodePudding user response:
Use zip
to iterate in parallel over the lists:
from statistics import mode
a = [[1,2,3], [1,3,3], [0,3,0]]
res = [mode(x) for x in zip(*a)]
print(res)
Output
[1, 3, 3]