![]() |
Help with array manipulation - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help with array manipulation (/thread-25170.html) |
Help with array manipulation - pberrett - Mar-22-2020 Hi all I have a relatively simple problem concerning arrays but I have read through several python training packages looking for an appropriate function without success Here is my problem. I have an array with various values in the array. let's say a = [[1,2,3],[4,5,6],[7,8,9]] I want to change the values in the array such that amounts under or over certain sizes are set to 0 i.e. a certain condition is met or not met. e.g. in the above example I want any value which is less than 3 and any value which is greater than 7 to be changed to zero. The changed array a should now look like this. [[0,0,3],[4,5,6],[7,0,0]] How do I do this in Python 3 code? One quick handicap. I don't want to use for loops. I want to do this as quickly and efficiently as possibly, possibly using numpy. Thanks Peter RE: Help with array manipulation - Larz60+ - Mar-22-2020 what have you tried so far? We will gladly help, but expect to see an attempt on your part first RE: Help with array manipulation - pberrett - Mar-28-2020 Sure and thanks Here's my test program # -*- coding: utf-8 -*- """ Created on Sat Mar 28 18:06:12 2020 @author: pberr """ #asetup array for test a = [[1,2,3],[4,5,6],[7,8,9]] #filter if a<3 or a>7: b=a else: b=0 #printout result print (b)The result was
RE: Help with array manipulation - buran - Mar-28-2020 at the moment you are comparing list with int as the error shows you need 2 nested loops: create empty list for your output b loop over sub-lists in your input list a inside the first loop: create a new empty list and append it to b create second loop to iterate over elements in current sub-list from the first loop for each element in the sub-list, do the test you want based on the result, append new value to last element (which is a list) of output list b Of course you can do this with list comprehension Because you say you want to do it with numpy - how would your data look like in numpy? RE: Help with array manipulation - pberrett - Mar-30-2020 Hi and thanks again The need for Numpy arises because I am processing a very large number of images. Each image is 1024 x 0124 and in its raw state has r,g,b values (color), each 256. I only use the red values in my analayis. This means that each image is 1024 x 1024 pixel with values at each pixel from 0 to 255. In Numpy thats a 1024 x 1024 array. Using nested loops is very inefficient and would be too slow. a in this example is meant to represent a numpy array, I'm not sure if I have represented or coded it correctly. a = [[1,2,3],[4,5,6],[7,8,9]] i.e [0][0] is 1, [1][3] is 7 etc The reason I asked the question is that having loaded a 1024 x 1024 image into a numpy array and excluded the green and blue values I now wanted to set the dimmer (say <100) and brighter (say >200) parts of the image to 0 and only do further processing on the pixels with mid range values. cheers Peter |