Python gets the most appearing elements in the list and the number of occurrences

Python gets the most appearing elements in the list and the number of occurrences

The max function is simply implemented eg: max(list, key=list.count)

li = ["要", "不要", "再考虑下", "要", "不要", "要"]
print(f"年轻人,你“{max(li, key=li.count)}”记得给博主点个赞哦!")
print(f"列表中出现次数最多的元素是:{max(li, key=li.count)} ,总出现次数:{li.count(max(li, key=li.count))}")

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44102466/article/details/111881165