I have a two list as follows
x = ['a','a','b','c','b','a']
and
x = ['a','a','b','c','c','d']
I’m trying to find which values occur the most in each of these lists. This is what I have tried.
def unique_values(output,input):
for i in input:
if i not in output:
output.append(i)
k = []
for i in k:
unique_values(k,x)
y.remove(i)
I’ve gotten this far but I cant figure out how to stop the for i in k: before it removes all the values in the list.
You can use
Countermodule fromcollections, if you want to find the occurrences of each element in the list: –So, the first two elements are most common in your list.
or, you also pass parameter to
most_common()to specify how manymost-commonelements you want: –Update : –
You can also find out the
maxcount first, and then find total number of elements with that value, and then you can use it as parameter inmost_common(): –