Python Forum
Help with array manipulation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with array manipulation
#1
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
Reply
#2
what have you tried so far?
We will gladly help, but expect to see an attempt on your part first
Reply
#3
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

Error:
runfile('C:/Users/pberr/Pictures/script.py', wdir='C:/Users/pberr/Pictures') Traceback (most recent call last): File "<ipython-input-34-60290e8d400d>", line 1, in <module> runfile('C:/Users/pberr/Pictures/script.py', wdir='C:/Users/pberr/Pictures') File "C:\Users\pberr\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace) File "C:\Users\pberr\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Users/pberr/Pictures/script.py", line 12, in <module> if a<3 or a>7: TypeError: '<' not supported between instances of 'list' and 'int'
Reply
#4
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?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  array manipulation divon 3 1,763 Sep-03-2021, 09:44 AM
Last Post: divon
  Array manipulation 0zMeister 7 2,815 Feb-15-2020, 12:34 AM
Last Post: 0zMeister
  N-Dim array manipulation in a loop, getting IndexError: too many indices for array cesardepaula 1 4,459 Mar-13-2019, 01:39 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020