Python 中的单个数组中的多个输入

问题描述 投票:0回答:1

我需要找到一个数组中最大的数字,它从用户那里获取输入并将其存储到数组变量中。通过将它们与每个数字进行比较,可以找到最大的数字。 如何解决这个问题?

我尝试使用输入()类型使用map和split()函数。循环遍历最终数组后,数组不会打印并返回最大的数字。Problem image

python arrays sorting arraylist
1个回答
0
投票

为什么需要使用数组?一个清单可能会更好。无论如何,这里是您问题的解决方案。

import numpy as np

#Create an array
numarr =np.array([1,2,3,4,5])

#Take user input and add it to an existing array
userinput=65
numarr=np.append(numarr, userinput, axis=None)

#Check to see if user input was added
print(numarr)



#Convert the array to a list
numarrlist=list(numarr)

#print the largest number
print(max(numarrlist))

#If you need the index location of the number in the original array
indexloc=np.where(numarr==(max(numarrlist)))
print(indexloc[0][0])

>>>[ 1  2  3  4  5 65]
>>>65
>>>5

这将返回包含用户输入的数组、数组中最大的数字以及数组中最大数字的索引位置。

© www.soinside.com 2019 - 2024. All rights reserved.