How do I replace values in 2D numpy array using a dictionary of {value:(row#,column#)} pairs

2024/10/13 13:20:24
 import numpy as np

the array looks like so:

 array = np.zeros((10,10))array = [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.][ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.][ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.][ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.][ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.][ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.][ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.][ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.][ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.][ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]

the dictionary is like this:

 dict = {72: (3, 4), 11: (1, 5), 10: (2, 4), 43: (2, 3), 22: (24,35), 11: (8, 9)}

I want to iterate over the array and replace any grid points that match the grid coordinates in the dictionary with the corresponding value in the dictionary

i am after an output like this:

 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.][ 0.  0.  0.  0.  0. 11.  0.  0.  0.  0.][ 0.  0.  0. 43. 10.  0.  0.  0.  0.  0.][ 0.  0.  0.  0. 72.  0.  0.  0.  0.  0.][ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.][ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.][ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.][ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.][ 0.  0.  0.  0.  0.  0.  0.  0.  0. 11.][ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]

** i have edited the question the provide co-ordinates that sit within the array except for 1 exception. I also provided an example of the desired output

Answer

I hope I understood your question correctly

array = np.zeros((10,10))
data = {72: (3, 4), 11: (1, 5), 10: (2, 4), 43: (2, 3), 22: (24,35)}for i in data.keys():try:array[data[i][0],data[i][1]] = float(i)except IndexError:pass
print array

I changed the indices such that it fits into your 10 x 10 array (I assume you work with a bigger array in your real example)

I iterate over all keys in the dictionary (the values). The program then tries to set this value in the array at the given coordinates. I pass IndexErrors for the case some coordinates are outside the array (like the last in this example.

EDIT

This solution only works if your keys are unique. If they are not I would recommend the solution of @Osssan.

https://en.xdnf.cn/q/118076.html

Related Q&A

Processing items with Scrapy pipeline

Im running Scrapy from a Python script.I was told that in Scrapy, responses are built in parse()and further processed in pipeline.py. This is how my framework is set so far:Python scriptdef script(self…

How to click a button to vote with python

Im practicing with web scraping in python. Id like to press a button on a site that votes an item. Here is the code<html> <head></head> <body role="document"> <div …

Python 2.7 connection to Oracle: loosing (Polish) characters

I connect from Python 2.7 to Oracle data base. When I use:cursor.execute("SELECT column1 FROM table").fetchall()]I have got almost proper values for column1 because all Polish characters (&qu…

getting friendlist from facebook graph-api

I am trying to get users friend list from facebook Graph-api. So after getting access token when I try to open by urlopen byhttps://graph.facebook.com/facebook_id/friends?access_token=authentic_access…

Sorting Angularjs ng-repeat by date

I am relatively new to AngularJS. Could use some helpI have a table with the following info<table><tr><th><span ng-click="sortType = first_name; sortReverse = !sortReverse&quo…

Html missing when using View page source

Im trying to extract all the images from a page. I have used Mechanize Urllib and selenium to extract the Html but the part i want to extract is never there. Also when i view the page source im not abl…

Move file to a folder or make a renamed copy if it exists in the destination folder

I have a piece of code i wrote for school:import ossource = "/home/pi/lab" dest = os.environ["HOME"]for file in os.listdir(source):if file.endswith(".c")shutil.move(file,d…

Segmentation fault after removing debug printing

I have a (for me) very weird segmentation error. At first, I thought it was interference between my 4 cores due to openmp, but removing openmp from the equation is not what I want. It turns out that wh…

numpy get 2d array where last dimension is indexed according to a 2d array

I did read on numpy indexing but I didnt find what I was looking for.I have a 288*384 image, where each pixel can have a labelling in [0,15]. It is stored in a 3d (288,384,16)-shaped numpy array im.Wit…

Error sending html email with mailgun python API

I can send text email with the Mailgun python API fine:def send_simple_message(mailtext, filename=""):requests.post("https://api.mailgun.net/v3/mydomain.in/messages",auth=("api…