-2

I have already read this question here, but I am not sure it is doing what I am looking for exactly.

Basically, I already have an existing list structure called points that I have read-in. If I do:

print points[0:2]

then I get

[x: -42.243 
y: 38.32432 
z: -9.3
x: 34.243 
y: -8.32432 
z: 21.3]

Now, simply what I would like to do, is generate a say, 6x1 random vector, and have its contents be directly copied into the [x y z x y z] values of the above list. I can generate a random array via:

import random
import numpy as np
randomArray = np.random.rand(6,1)

, but how do I copy it's contents INTO points exactly?

Thanks!

Community
  • 1
  • 1
TheGrapeBeyond
  • 543
  • 2
  • 8
  • 19
  • 2
    What does `print type(points[0])` print? – shx2 Nov 06 '15 at 20:22
  • 2
    can you show what your actual output for `print points[0:2]` is? because right now what you say you have is not making much sense. is the list content a single string? is it a dict of values? – R Nar Nov 06 '15 at 20:27
  • @RNar If I do that, then I get: `` – TheGrapeBeyond Nov 06 '15 at 20:28
  • if each item in `points` is a custom class type, please show what that type is – R Nar Nov 06 '15 at 20:31
  • @RNar Let me be clear: If I do `print type(points)`, I will get ``. If I do `print type(points[0])`, I will get ``. If I do `print type(points[0:1])`, I will get ``. Finally for what it's worth, I happen to know that the coordinate is simply 3 doubles. Thanks. – TheGrapeBeyond Nov 06 '15 at 20:35
  • and how would you normally set values for your `coordinate` class? – R Nar Nov 06 '15 at 20:37
  • @RNar That is done in C++. I am reading a message from this class in C++, but this is how it manifests itself in python. – TheGrapeBeyond Nov 06 '15 at 20:39
  • Why the downvote? What is not clear? – TheGrapeBeyond Nov 06 '15 at 20:48
  • what do you mean by manifests itself in python? why dont you do it in C++ if your class is in C++? – R Nar Nov 06 '15 at 20:52
  • @RNar Ok, I am GIVEN this data structure - I have no control over it. I am HANDED this over, and I am asked to manipulate it in Python. But I happen to know, that coordinate is in fact just 3 doubles. – TheGrapeBeyond Nov 06 '15 at 20:53
  • All these questions indicate that your question is not clear. The big question is, just what is this `coordinate` class, and how do you set its values in Python? Your print expression is not 'just 3 doubles', or 2 sets of 3. – hpaulj Nov 06 '15 at 20:55
  • yes okay sorry, i was just clarifying. how do you access the values for coordinates? is it a tuple, a list, a dict, etc? can you do `coordinate['x']` to get the x or `coordinate[0]` or `coordinate.x`? as @hpaulj said, this is what it means by unclear because without this knowledge, we cannot do much to help you – R Nar Nov 06 '15 at 20:56
  • @hpaulju I do not understand your question to be honest with you. I got handed a file. Here it is, and it has points. That's it. I want to manipulate it's values. – TheGrapeBeyond Nov 06 '15 at 21:02
  • @RNar Yes, if I do `print points[0].x`, then I will get its value, yes. Same with y and z... – TheGrapeBeyond Nov 06 '15 at 21:03
  • I just edited your question, giving proper indention to the printed block. That makes it clearer that what is being printed is not any ordinary list. In your last comment you mention a *file*. I don't see a file in the question, not even something labeled as a clip from a file. Is this a text file that you read, or a binary file, or what? – hpaulj Nov 06 '15 at 21:10
  • @hpaulj I am reading this from a ROS bag file. This ROS file has a data structure called points. – TheGrapeBeyond Nov 06 '15 at 21:11
  • I added a `ROS` tag. You are asking the wrong people. – hpaulj Nov 06 '15 at 21:19

2 Answers2

1

Not knowing much about your coordinate class, maybe this works?

num_points = 2
random_array = np.random.rand(num_points, 3)
for i, point in enumerate(random_array):
    points[i] = coordinate(*point)
shx2
  • 61,779
  • 13
  • 130
  • 153
1

To give you a sense of how the copying will depend on the elements in that list, I'll demonstrate with several known types of elements:

First make a simple 'random' set of values, aranged in 2 sets of 3 (a 2x3 array):

In [213]: randomArray=np.arange(6).reshape(2,3)

If points is also a 2d array, copying values to two of its rows is trivial:

In [214]: points = np.zeros((4,3),int)    
In [215]: points[:2,:]=randomArray

In [216]: points
Out[216]: 
array([[0, 1, 2],
       [3, 4, 5],
       [0, 0, 0],
       [0, 0, 0]])

Instead if points is a list of lists, then we have to copy sublist by sublist, row by row. I'll use zip to coordinate that. I could also use indexing, r[i][:] = x[i,:].

In [217]: points = [[0,0,0] for _ in range(3)]
In [218]: points
Out[218]: [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
In [219]: for r,x in zip(points[:2],randomArray):
   .....:     r[:] = x

In [220]: points
Out[220]: [[0, 1, 2], [3, 4, 5], [0, 0, 0]]

Let's try a list of tuples:

In [221]: points = [(0,0,0) for _ in range(3)]
In [222]: for r,x in zip(points[:2],randomArray):
    r[:] = x
   .....:     
...    
TypeError: 'tuple' object does not support item assignment

Can't do that kind of inplace change to tuples. I'd have to replace them, with something like, points[0] = tuple(randomArray[0]), etc.

How about a list of dictionaries?

In [223]: points = [{'x':0,'y':0,'z':0} for _ in range(3)]    
In [224]: points
Out[224]: [{'x': 0, 'y': 0, 'z': 0}, {'x': 0, 'y': 0, 'z': 0}, {'x': 0, 'y': 0, 'z': 0}]

In [225]: for r,x in zip(points[:2],randomArray):
    r.update({'x':x[0],'y':x[1],'z':x[2]})
   .....:     
In [226]: points
Out[226]: [{'x': 0, 'y': 1, 'z': 2}, {'x': 3, 'y': 4, 'z': 5}, {'x': 0, 'y': 0, 'z': 0}]

I have to construct another dictionary from the row, and use dictionary .update. Or r['x']=x[0]; r['y']=x[1]; etc.

Notice that all of these points displays differently from your example. Your list must consist of objects that I know nothing about - except that its str() method shows values in a vaguely dictionary like manner. That display tells me nothing about how they can be modified, if at all.

You mention in a comment the source of this list is a ROS .bag file. Then you must have read this file with some imported module. Something like rospy? That kind of information is important. How was the list created?

hpaulj
  • 221,503
  • 14
  • 230
  • 353