簡體   English   中英

如何從數組中輸入最高值的最高值打印出來? 在Python 3中

[英]How do I print the highest of an inputted value from an array, which has the same highest value? In Python 3

我希望這個問題有意義,因為我不太確定該怎么問。

但是我的程序-使用python-要求用戶以數組形式輸入他們在這些冰淇淋口味中的1-10分。 顯示其分數,然后將其最高分數打印為他們最喜歡的味道。 它獲取索引數並從數組中打印出味道。 但是,假設用戶將Mint Choc Chip和Strawberry都設置為10。該程序只會首先打印數組中的項目,這是min choc chip,盡管草莓也是最高分。 有誰知道一種使程序顯示所有得分最高的風味的方法? 請記住,我是Python的新手,所以如果答案對您而言顯而易見,那么它對我來說不是。 因此,請客氣,任何幫助或建議將不勝感激!

我嘗試添加以下內容: 如果高> 1:打印(“ \\ n您最喜歡的冰淇淋口味是”,風味[高],風味[高])

但是只需打印兩次出現在陣列中的相同味道,就可以打印:您最喜歡的冰淇淋味道是薄荷脆片薄荷脆片。 而且我知道這沒有意義,因為如果最高分是三種口味,則只會打印兩種(相同)。 我也曾嘗試尋找其他功能,例如導入等。但是我找不到一個有用的功能。 這對於程序來說不是必需的,但是會使它變得更好,更現實。 謝謝!

碼:

import numpy as np

flavours = ["Vanilla", "Chocolate", "Mint Choc Chip", "Rosewater", "Strawberry", "Mango", "Frutti Tutti", "Fudge Brownie", "Bubblegum", "Stracciatella"]

Ice_Cream= [0]*10

print("Please input your score on these flavours of ice cream. With 1 being the lowest and 10 the highest.\n")

for i in range(0,10):
    print(flavours[i]+":")
    Ice_Cream[i] = int(input())

print("\nResults:\n")

for i in range(0,10):
      print(flavours[i]+":",Ice_Cream[i])

high = np.argmax(Ice_Cream)

if high > 1:
  print ("\nYour favourite flavour of ice cream is", flavours[high], flavours[high])

else:

  print ("\nYour favourite flavour of ice cream is", flavours[high])

Numpy.argmax返回最大值索引的數組 https://docs.scipy.org/doc/numpy-1.14.0/reference/generation/numpy.argmax.html

你應該遍歷它

for i in np.nditer(high):
    print ("\nYour favourite flavour of ice cream is", flavours[i])
high=[i for i, x in enumerate(Ice_Cream) if x == max(Ice_Cream)]
for i in high:
  print ("Your favourite flavour of ice cream is", flavours[i])

高變量存儲所有冰淇淋最大值的指數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM