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?