我是靠谱客的博主 腼腆画板,这篇文章主要介绍python笔试题之找出一个列表里出现频次最高的元素(most common elements in a list),现在分享给大家,希望可以做个参考。

def most_common(seq):
    d = {}
    for i in seq:
       d[i] = d.get(i, 0) + 1
    ret = []
    for j in sorted(d.items(), reverse=True, key=lambda x:x[1]):
        if len(ret) == 0:
            ret.append(j[0])
            n = j[1]
        else:
            if j[1] == n:
                ret.append(j[0])
            else:
                break
    return ret
print '频次最高', most_common([1,2,3,4,5,1,1,2,2])

最后

以上就是腼腆画板最近收集整理的关于python笔试题之找出一个列表里出现频次最高的元素(most common elements in a list)的全部内容,更多相关python笔试题之找出一个列表里出现频次最高的元素(most内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部