我有一个列表,一旦我对它进行排序以找到“最大值”,我需要显示输入该数字的位置。
知道怎么做吗?
我编写了这段代码来接收和排序列表,但我不知道如何接收“数据”的初始位置。
liste = []
while len(liste) != 5:
liste.append (int(input("enter number :\n")))
listeSorted = sorted(liste)
print("the biggest number is : ", listeSorted[-1])
最佳答案
不是通过对列表进行排序来查找列表的最大值,而是使用 max 函数:
largest_val = max(list)
要查找列表中第一次出现的值,请使用 .index
方法。
position = list.index(largest_val)
关于python - 如何显示输入的数字在哪个位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49720942/