简体   繁体   English

在列表中找到最小值

[英]Find smallest value in list

Very simple program here. 这里的程序非常简单。 I'm trying to take a positive integer n, generate a list of n random numbers between 100 and 500, and return the minimum value that appears in the list 我正在尝试取一个正整数n,生成一个介于100和500之间的n个随机数的列表,并返回列表中显示的最小值

import random
def randomNumber2(n):
    mylist2 = []
    needMoreNumbers2 = True
    while (needMoreNumbers2):
        randomNumber2 = int(random.random() * 500)
        myList2.append(randomNumber2)
        n >= n - 100
        if (n <= 500):
            needMoreNumbers2 = false
    return myList2

Could someone help guide me in the direction I need to go? 有人可以指导我朝着我需要去的方向前进吗?

import random

def randomNumber2(n):
    return min(random.randint(100, 500) for _ in range(n))

If you want something closer to your program, you need to fix a number of things in your program first - it doesn't run at all. 如果你想要更接近程序的东西,你需要先在程序中修复一些东西 - 它根本不运行。 Python is case-sensitive, so mylist2 and myList2 are different. Python区分大小写,因此mylist2myList2不同。 Use the same spelling throughout. 在整个过程中使用相同的拼写。 Similarly, false is not a Python builtin - False is. 同样, false也不是Python内置的 - False是。

Fix those and we get: 解决这些问题,我们得到:

import random

def randomNumber2(n):
    myList2 = []
    needMoreNumbers2 = True
    while (needMoreNumbers2):
        randomNumber2 = int(random.random() * 500)
        myList2.append(randomNumber2)
        n >= n - 100
        if (n <= 500):
            needMoreNumbers2 = False
    return myList2

Now regardless of what's passed for n , so long as n is <= 500 that returns a list with a single random integer, in 0 thru 499 (inclusive). 现在不管n传递了什么,只要n <= 500就返回一个带有单个随机整数的列表,从0到499(含)。 If n is > 500, it runs forever (infinite loop). 如果n > 500,它将永远运行(无限循环)。 Can you see why? 你能明白为什么吗? if (n <= 500) is always true if the original n is <= 500, and always false otherwise. if (n <= 500)如果原始n <= 500则始终为true,否则始终为false。 Your n >= n - 100 doesn't do anything visible (it just compares n to n - 100 , and always returns true, which is ignored). 你的n >= n - 100没有做任何可见的事情(它只是将nn - 100进行比较,并且总是返回true,这将被忽略)。

So take the advice to move toward more reasonable Python idioms. 因此,请采取建议,以转向更合理的Python习语。 Here's a way that should be easier to understand: 这是一种应该更容易理解的方式:

import random

def randomNumber2(n):
    myList2 = []
    while len(myList2) < n:
        randomNumber2 = int(random.random() * 500)
        myList2.append(randomNumber2)
    return myList2

Progress ;-) Now we at least get back a list with n random integers. 进展;-)现在我们至少得到一个包含n随机整数的列表。 They're not in the range you want, though: int(random.random() * 500) returns an int in the range 0 through 499 inclusive. 但它们不在您想要的范围内: int(random.random() * 500)返回0到499(包括0和499)范围内的int。 If you want them to be between 100 and 500 inclusive, the best way is to do random.randint(100, 500) , as someone else already showed you. 如果你希望它们介于100和500之间,那么最好的方法是执行random.randint(100, 500) ,就像别人已经给你random.randint(100, 500)那样。 100 + int(random.random() * 400) is an equivalent way that's closer to your original code, so let's try that: 100 + int(random.random() * 400)是一种与原始代码更接近的等价方式,所以让我们试试:

import random

def randomNumber2(n):
    myList2 = []
    while len(myList2) < n:
        randomNumber2 = 100 + int(random.random() * 400)
        myList2.append(randomNumber2)
    return myList2

To get the minimum, it's overwhelmingly best to just do min(myList2) at the end. 为了获得最小值,最后只需要做min(myList2)可以了。 Not only will that be correct, it's also faster than anything you can code yourself. 这不仅是正确的,而且比你自己编码的任何东西都要快。 If you want to do it yourself, here's one way: 如果你想自己做,这是一种方式:

import random

def randomNumber2(n):
    myList2 = []
    smallest = None
    while len(myList2) < n:
        randomNumber2 = 100 + int(random.random() * 400)
        myList2.append(randomNumber2)
        if smallest is None or randomNumber2 < smallest:
            smallest = randomNumber2
    assert smallest == min(myList2)
    return myList2

The answer from FJ is how most experienced Pythonistas would code it, though. 不过,FJ的答案是大多数经验丰富的Pythonistas会对它进行编码。

I'm trying to take a positive integer n, generate a list of n random numbers between 100 and 500, and return the minimum value that appears in the list 我正在尝试取一个正整数n,生成一个介于100和500之间的n个随机数的列表,并返回列表中显示的最小值

Then, you don't need the list, you just need to return the smallest of the random numbers generated in n "throws" 然后,你不需要列表,你只需要返回n “throws”中生成的最小随机数

import random

def smallrand(n,rangemin=100,rangemax=500):
    smallest = float('+inf')
    for _ in xrange(n):
       new = random.randint(rangemin, rangemax)
       if new < smallest: smallest = new
    return smallest

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

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