Hey, I've got an array full of random numbers and I want to say if the array is below a certain value, say 15, then to make everything in that array that less than 15 equal to zero. So far I've tried a few things and they've not worked, and I'm sure it's a pretty simple answer, but I haven't found one yet.

Many thanks,


7 Years
Discussion Span
Last Post by woooee
from random import randint

array = [randint(0, 30) for i in range(15)]
print (array)
for i in range(15):
    if array[i] < 15: array[i] = 0
print (array)

Hey, I've got an array full of random numbers

It depends on how many levels are in the array. Generally, you would flatten the array into a single level list, and operate on that. If you want to keep the original structure, then you have to test for the type. If it is an integer you can modify if. If it is a list, then you have to traverse the inner list. Recursion would probably be the best choice for doing that.

Edited by woooee: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.