查找列表的模式 [英] Finding the mode of a list

查看:50
本文介绍了查找列表的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个项目列表,回想一下列表的模式是最常出现的项目.

Given a list of items, recall that the mode of the list is the item that occurs most often.

我想知道如何创建一个函数,该函数可以找到列表的模式,但如果列表没有模式则显示一条消息(例如,列表中的所有项目只出现一次).我想在不导入任何函数的情况下制作这个函数.我正在尝试从头开始创建自己的函数.

I would like to know how to create a function that can find the mode of a list but that displays a message if the list does not have a mode (e.g., all the items in the list only appear once). I want to make this function without importing any functions. I'm trying to make my own function from scratch.

推荐答案

您可以使用 max 函数和一个键.看看 python max function using 'key' 和 lambda 表达式.

You can use the max function and a key. Have a look at python max function using 'key' and lambda expression.

max(set(lst), key=lst.count)

这篇关于查找列表的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆