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,

furret

Recommended Answers

All 2 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.