Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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!

share|improve this question
2  
What does print type(points[0]) print? – shx2 11 hours ago
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 11 hours ago
    
@RNar If I do that, then I get: <type 'list'> – TheGrapeBeyond 11 hours ago
    
if each item in points is a custom class type, please show what that type is – R Nar 11 hours ago
    
@RNar Let me be clear: If I do print type(points), I will get <type, 'list'>. If I do print type(points[0]), I will get <class, 'coordinate'>. If I do print type(points[0:1]), I will get <type, 'list'>. Finally for what it's worth, I happen to know that the coordinate is simply 3 doubles. Thanks. – TheGrapeBeyond 11 hours ago

2 Answers 2

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)
share|improve this answer

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?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.