1

Sorry if the question is already answered. I couldn't figure out a term to use to search this.

I have a bunch of list coordinates in a list, like the one below.

coordinate = [[0,0,0,0], [1,1,1,1,]]

I have to use this to change a value in an array for numpy. (I didn't use numpy for the title because it can be used for normal lists too)

To change a value in numpy I have to run

array[0,0,0,0]=value

How do I make array[0,0,0,0] from array and [0,0,0,0]? I know I how to use strings to make a string that looks like that, but can I use that string to run a function for slice an index?

AMC
  • 2,642
  • 7
  • 13
  • 35
Moses Kim
  • 242
  • 3
  • 16
  • 2
    Sorry but I am confused with your question? What are you trying to change? Can you provide more details along with some examples in your question? – Error - Syntactical Remorse Oct 15 '19 at 18:11
  • 1
    I'm trying to make a command ```array[0,0,0,0]``` by using a string ```array``` and a list ```[0,0,0,0]``` – Moses Kim Oct 15 '19 at 18:12
  • 2
    Thats not very easy. You can use `eval` but I DONT recommend it. You could probably use tuple unpacking with numpy such as: `array[*coordinate[0]] = value`. There is no really "safe" way to turn strings into raw syntax. – Error - Syntactical Remorse Oct 15 '19 at 18:13
  • 4
    Convert the index list to a tuple: `array[tuple(coordinate[0])]` – Paul Panzer Oct 15 '19 at 18:13
  • @PaulPanzer This is mind blowing! Thank you! – Moses Kim Oct 15 '19 at 18:16
  • 3
    Please put your actual inputs. There is no need for ambiguity – juanpa.arrivillaga Oct 15 '19 at 18:17
  • Why does this the title mention strings? I don't see what they have to do with this question. – AMC Oct 15 '19 at 18:55
  • Also, a brief search of SO shows that this question has already been asked, and answered, multiple times. – AMC Oct 15 '19 at 19:03
  • @AlexanderCécile The question mentions strings because he's trying to turn strings into raw syntax. – John Oct 15 '19 at 20:26
  • @John how can you tell that he's doing that, has he edited the question? His indexes are inside of a nested list of ints, I can't see where strings actually play a role. – AMC Oct 15 '19 at 20:38
  • @AlexanderCécile his response to the first comment: "I'm trying to make a command array[0,0,0,0] by using a string array and a list [0,0,0,0]" – John Oct 15 '19 at 21:38
  • @John yes, but from looking at his post it seems quite clear to me that that is not what he is actually trying to do, he just misspoke. – AMC Oct 15 '19 at 21:42
  • @AlexanderCécile It's not clear to me why we should disregard what little clarification he's provided. The question should definitely be edited to make it more clear but why do you think he's attempting to do something other than the thing he clearly stated he was attempting to do? – John Oct 15 '19 at 21:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/200922/discussion-between-alexander-cecile-and-john). – AMC Oct 15 '19 at 21:50

1 Answers1

0

With test array:

In [163]: arr = np.arange(24).reshape(2,3,4) 

selecting an item:

In [168]: arr[0,1,2]                                                            
Out[168]: 6

selecting the same item with a index list:

In [169]: idx = [0,1,2]                                                         
In [170]: arr[tuple(idx)]                                                       
Out[170]: 6

arr[0,1,2] is implicitly arr[(1,2,3)]. The Python interpreter passes those comma separated index numbers as a tuple to the array's __getitem__ method.

This multidimensional indexing does not work with lists.

In [171]: alist = arr.tolist()                                                  
In [172]: alist[0][1][2]                                                        
Out[172]: 6

For several points:

In [180]: idx = np.array([[0,1,2],[1,2,3]])                                     
In [181]: idx                                                                   
Out[181]: 
array([[0, 1, 2],
       [1, 2, 3]])
In [182]: tuple(idx.T)                                                          
Out[182]: (array([0, 1]), array([1, 2]), array([2, 3]))
In [183]: arr[tuple(idx.T)]                                                     
Out[183]: array([ 6, 23])

In effect I am passing each column of idx as an indexing array (for each dimension).

hpaulj
  • 221,503
  • 14
  • 230
  • 353