Python list. I'm asking you a question

Asked 2 years ago, Updated 2 years ago, 17 views

There are many numbers on the list, but how do you find the most duplicated numbers? For example, I want to know how to find the most number 6 in the list [1,1,1,1,3,4,5,5,6,6,6,6] that comes out four times.

python

2022-09-20 22:23

1 Answers

There are many ways.

import itertools as it

L = [1,1,1,3,4,5,5,6,6,5,5,5,6,6]
item_cnt = {k: len(list(v)) for k, v in it.groupby(sorted(L))}
max(item_cnt.keys(), key=lambda n:item_cnt[n])
Out[46]: 5


2022-09-20 22:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.