简体   繁体   English

Python Min-Max函数-列出作为返回min和max元素的参数

[英]Python Min-Max Function - List as argument to return min and max element

Question: write a program which first defines functions minFromList(list) and maxFromList(list). 问题:编写一个程序,该程序首先定义函数minFromList(list)和maxFromList(list)。 Program should initialize an empty list and then prompt user for an integer and keep prompting for integers, adding each integer to the list, until the user enters a single period character. 程序应初始化一个空列表,然后提示用户输入整数,并继续提示输入整数,将每个整数添加到列表中,直到用户输入单个句点字符为止。 Program should than call minFromList and maxFromList with the list of integers as an argument and print the results returned by the function calls. 然后,程序应使用整数列表作为参数调用minFromList和maxFromList,并打印函数调用返回的结果。

I can't figure out how to get the min and max returned from each function separately. 我无法弄清楚如何分别获得每个函数的最小值和最大值。 And now I've added extra code so I'm totally lost. 现在,我添加了额外的代码,所以我完全迷路了。 Anything helps! 有什么帮助! Thanks! 谢谢!

What I have so far: 到目前为止,我有:

def minFromList(list)

texts = []
    while (text != -1):
    texts.append(text)

high = max(texts)

return texts 

def maxFromList(list)

texts []
    while (text != -1):
    texts.append(text)

low = min(texts)

return texts



text = raw_input("Enter an integer (period to end): ")
list = []
while text != '.':
    textInt = int(text)
    list.append(textInt)
    text = raw_input("Enter an integer (period to end): ")

print "The lowest number entered was: " , minFromList(list)
print "The highest number entered was: " , maxFromList(list) 

I think the part of the assignment that might have confused you was about initializing an empty list and where to do it. 我认为作业中可能使您感到困惑的部分是关于初始化一个空列表以及在何处执行。 Your main body that collects data is good and does what it should. 您收集数据的主体是好的并且会做应做的事情。 But you ended up doing too much with your max and min functions. 但是最终您在max和min函数上做了太多事情。 Again a misleading part was that assignment is that it suggested you write a custom routine for these functions even though max() and min() exist in python and return exactly what you need. 再次有一个令人误解的部分是赋值是建议您即使在python中存在max()和min()并返回所需的内容时,也建议您为这些函数编写一个自定义例程。

Its another story if you are required to write your own max and min, and are not permitted to use the built in functions. 如果您需要编写自己的max和min,并且不允许使用内置函数,则这是另一个故事。 At that point you would need to loop over each value in the list and track the biggest or smallest. 此时,您需要遍历列表中的每个值并跟踪最大或最小值。 Then return the final value. 然后返回最终值。

Without directly giving you too much of the specific answer, here are some individual examples of the parts you may need... 在没有直接给您太多具体答案的情况下,以下是您可能需要的零件的一些单独示例...

# looping over the items in a list
value = 1
for item in aList:
    if item == value:
        print "value is 1!"

# basic function with arguments and a return value
def aFunc(start):
    end = start + 1
    return end
print aFunc(1)
# result: 2

# some useful comparison operators
print 1 > 2  # False
print 2 > 1  # True

That should hopefully be enough general information for you to piece together your custom min and max functions. 希望这应该是足够的常规信息,可以让您组合自定义的最小和最大函数。 While there are some more advanced and efficient ways to do min and max, I think to start out, a simple for loop over the list would be easiest. 尽管有一些更先进,更有效的方法来进行最小和最大运算,但我认为应该从最简单的for循环列表开始。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM