0

我正在尝试编写一个 python 程序,它将打印一个随机数数组 1 thur 100,并打印出数组的最大值。这是我到目前为止所拥有的:

import random
import timeit
print [random.randint(0,100) for r in xrange(10)]
print "Max number in array is",
4

3 回答 3

1

将随机数保存在变量中,data. 然后用取最大值max(data)

data = [random.randint(0,100) for r in xrange(10)]
print(data)
print("Max number in array is {}".format(max(data)))
于 2013-07-21T21:09:21.107 回答
1

您需要将生成的数组存储在变量中,然后使用max它来获取最大值;

import random
import timeit
my_array = [random.randint(0,100) for r in xrange(10)]
print my_array
print "Max number in array is", max(my_array)
于 2013-07-21T21:09:31.203 回答
1
import random
rand_ar = [random.randint(1,100) for r in xrange(10)] # randint(0,100) includes 0 also.
print rand_ar
print "Max number in array is %i" % max(rand_ar)
于 2013-07-21T21:10:35.267 回答