【问题标题】:The occurrence of the biggest number in a list [duplicate]列表中最大数字的出现[重复]
【发布时间】:2019-10-29 10:18:05
【问题描述】:

所以,在互联网上搜索了几个小时后,虽然我找到了一些相关的东西,但仍然找不到我想要的东西。在我的代码中,我提供了 6 个输入。在该输入之后,我设法将它们插入到 list() 中,并使用 max() 找到最大的数字。我需要找到频率(这个数字重复了多少次,而不是那个数字是多少)。在谷歌中,我发现重复次数最多的数字是什么,但没有重复多少次。例如,如果用户输入 1,1,2,2,2,3 ,那么 max() 的第一个输出已经是 3。那么如何获得最大数字 3 的频率呢,它只重复一次。所以我正在寻找的输出是 1。它看起来像这样: 最大的数字是:3 出现的最大数是:1

I have already managed to get biggest number output as I mentioned, but not the occurrence. Waiting for your response:    
Number_1 = int(input("Please type your 1st number: "))
Number_2 = int(input("Please type your 2nd number: "))
Number_3 = int(input("Please type your 3rd number: "))
Number_4 = int(input("Please type your 4th number: "))
Number_5 = int(input("Please type your 5th number: "))
Number_6 = int(input("Please type your 6th number: "))

listNumbers = [Number_1, Number_2, Number_3, Number_4, Number_5, Number_6]


maxNumber = max(listNumbers)
print("The largest number is ", maxNumber)

#Most occurrent
def most_frequent(List):
    return max(set(List), key = List.count)

*THE OCCURRENCE NUMBER CODE GOES HERE*

【问题讨论】:

标签: python python-3.x find-occurrences


【解决方案1】:

只计算你已经找到的最大数量:

maxNumberCount = listNumbers.count(maxNumber)
print(maxNumberCount)

【讨论】:

  • 是的,已经解决了,非常感谢!
【解决方案2】:
print(listNumbers.count(max(listNumbers)))

就这么简单。 ?

【讨论】:

  • 是的,已经解决了,非常感谢!
  • 你可以接受它@neverbeagod。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-26
  • 2018-05-08
  • 1970-01-01
  • 2021-03-30
  • 2017-02-13
  • 2023-03-30
相关资源
最近更新 更多