Chris Sanders
Chris Sanders

Reputation: 1

Trying to find the max number in a python array?

I'm trying to write a python program that will print me a random numbers array 1 thur 100, and also print out the max value of the array. This is what I have so far:

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

Upvotes: 0

Views: 267

Answers (3)

rnbguy
rnbguy

Reputation: 1399

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)

Upvotes: 1

Joachim Isaksson
Joachim Isaksson

Reputation: 181077

You'll need to store the array as generated in a variable, and just use max on it to get the maximum value;

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)

Upvotes: 1

unutbu
unutbu

Reputation: 880907

Save the random numbers in a variable, data. Then take the max with max(data).

data = [random.randint(0,100) for r in xrange(10)]
print(data)
print("Max number in array is {}".format(max(data)))

Upvotes: 1

Related Questions