简体   繁体   English

如果最大的数字在同一个列表中多次出现,我如何将它们全部打印出来?

[英]If the largest number occurs more than once in the same list, how do I print them all out?

Here is my program,这是我的程序,

empty_list = []
max_no = 0
for i in range(5):
    input_no = int(input("Enter a number: "))
    empty_list.append(input_no)
for x in empty_list:
    if x > max_no:
       max_no = x
high = empty_list.index(max_no)
print ([empty_list[high]])

Example list: [4, 3, 6, 9, 9]示例列表: [4, 3, 6, 9, 9]

Example output: [9]示例 output: [9]

How can I change my program to print out all the instances of the largest number occurring in the same list?如何更改我的程序以打印出同一列表中出现的最大数量的所有实例?

Expected output: [9, 9]预期 output: [9, 9]

In order to find the biggest number, you can use the Python built-in function max()为了找到最大的数,可以使用 Python 内置 function max()

max_no = max(empty_list)

In order to count them, you can use the count() method, like so:为了计算它们,您可以使用count()方法,如下所示:

count_no = empty.count(max_no)

You can store both the maximum number and the number of occurrences of that number.您可以存储该数字的最大数量和出现次数。

empty_list = []
max_no = 0
times = 0
for i in range(5):
    input_no = int(input("Enter a number: "))
    empty_list.append(input_no)
for x in empty_list:
    if x > max_no:
       max_no = x
       times = 1;
    elif x == max_no:
        times += 1
print([max_no] * times)

Demo演示

You are asking, given a list with integers, you want a function that你在问,给定一个整数列表,你想要一个 function
returns a list with the largest integer N number of times where N is the返回具有最大 integer N 次的列表,其中 N 是
number of occurrence in the given list.给定列表中的出现次数。

def foo(lst):
    r = max(lst) # find maximum integer in the list
    d = lst.count(r) # find how many times it occurs in the list
    return [r for i in range(d)] # create a list with the max int N number of times.
    
lst = [1,1,1,1,5,5,9]
print(foo(lst)) # prints [9]

lst = [1,1,1,1,5,5]
print(foo(lst)) # prints [5,5]

lst = [4, 3, 6, 9, 9]
print(foo(lst)) # prints [9,9]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅当它们在 python 的列表中出现多次时,我如何打印值 - How do I print values only when they appear more than once in a list in python 如何获取Python以显示所有可能的余数,而不是仅显示数字最大倍数的余数? - How do i get Python to show all possible remainders instead of just the one that occurs with the largest multiple of the number? 我如何打印出所有列表 - How do I print out all the list 如何找到一个单词(字符串)是否在 python 的输入/列表中出现多次 - How can I find if a word (string) occurs more than once in an input/list in python 在python中,如何使用for循环查找在句子中出现多次的单词的位置? - In python, how do I find the positions of a word that occurs more than once in a sentence using a for loop? 如果我将多个相同的事物添加到列表中,并且想要摆脱其中的一个,我怎么知道它是哪个? - If I append more than one of the same thing to a list and i want to get rid of one of them how do i know which one it is? 如何获得程序以打印列表中出现数字的次数? - How do I get the program to print the number of times a number occurs in the list? 如何从 django 中的同一个表单字段获取所有输入? (表单字段在 HTML 中循环,因此出现不止一次) - How do I get all the inputs from the same form field in django? (The form field is in a loop in the HTML thus appears more than once) 如何从基于3个列表的特定索引输出输出(所有3个列表在相同位置上) - How do I print out an output from a specific index based upon 3 Lists (same position on list for all 3) Python 如何按顺序多次打印列表? - Python how to print a list more than once but in order?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM