追加/提取列表元素的有效途径 [英] Efficient way of appending / extracting list elements

查看:142
本文介绍了追加/提取列表元素的有效途径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下列表:

rays_all = [np.array(r11, r21, r31, r41),
            np.array(r12, r22, r32, r42),
            np.array(r13, r23, r33, r43),
            np.array(r14, r24, r34, r44)]

所有的R11,R21,R31等与形状(3L,)阵列(认为它是在三维空间中的向量)。

all the r11, r21, r31, etc are arrays with shape (3L,) (think of it as a vector in 3D space).

如果我想提取 np.array的(4L,3L)阵列(R14,R24,R34,R44),我只是简单地使用 rays_all [-1] 。如果我要追加 np.array(R15,R25,R35,R45),我只是用 rays_all.append

If I want to extract the (4L,3L) array of np.array(r14, r24, r34, r44), I just simply use rays_all[-1]. If I want to append a new array of np.array(r15, r25, r35, r45), I just use rays_all.append.

现在我以另一种方式安排上述载体(R11,R12,等):

Now I arrange the above vectors (r11,r12, etc) in an alternative way:

ray1 = [r11, r12, r13, r14]
ray2 = [r21, r22]
ray3 = [r31, r32, r33]
ray4 = [r41, r42, r43, r44]

每个'线',现在有自己的名单有不同的长度。如果我想提取每个列表的最后一个数组中的元素结构,即 np.array([R14,R22,R33,R44]),什么是最有效的办法这样做呢?在另一方面,如果我想添加的元素数组中的 np.array([R15,R23,R34,R45])到列表中,这样我将有

Each 'ray' now has its own list with different lengths. If I want to extract the last element of each list in an array structure, i.e. np.array([r14,r22,r33,r44]), what is the most efficient way to do so? On the other hand, if I want to add the elements in the array np.array([r15,r23,r34,r45]) to the list such that I will have

ray1 = [r11, r12, r13, r14, r15]
ray2 = [r21, r22, r23]
ray3 = [r31, r32, r33, r34]
ray4 = [r41, r42, r43, r44, r45]

什么是最有效的方法是什么?我知道我可以只是一个循环,这样做,但我想这比 rays_all [-1] rays_append慢得多()?是否有这样做的任何矢量的方式?

what is the most efficient way? I know I can just a loop to do so, but I guess it is much slower than the rays_all[-1] and rays_append()? Are there any 'vectorized' way of doing this?

推荐答案

小心混合阵列和列表操作。

Be careful with mixing array and list operations.

请一些3单元阵列,并将它们合并为你的第一种情况:

Make some 3 element arrays and combine them as in your first case:

In [748]: r1,r2,r3,r4=np.arange(3),np.ones(3),np.zeros(3),np.arange(3)[::-1]
In [749]: x1=np.array((r1,r2))
In [750]: x2=np.array((r3,r4))
In [751]: rays=[x1,x2]
In [752]: rays
Out[752]: 
[array([[ 0.,  1.,  2.],
        [ 1.,  1.,  1.]]), array([[ 0.,  0.,  0.],
        [ 2.,  1.,  0.]])]

射线现在列表包含两个二维数组((2,3)形状)。正如你所说,你可以选择从该列表中的项目或追加另一个数组它(您可以附加任何东西给它,不只是一个类似的数组)。的射线操作列表操作。

rays is now a list contain two 2d array ((2,3) shape). As you say, you can select an item from that list or append another array to it (you can append anything to it, not just a similar array). Operations of rays are list operations.

您还可以创建一个三维数组:

You could also create a 3d array:

In [758]: ray_arr=np.array((x1,x2))
In [759]: ray_arr
Out[759]: 
array([[[ 0.,  1.,  2.],
        [ 1.,  1.,  1.]],

       [[ 0.,  0.,  0.],
        [ 2.,  1.,  0.]]])
In [760]: ray_arr.shape
Out[760]: (2, 2, 3)
In [761]: ray_arr[-1]
Out[761]: 
array([[ 0.,  0.,  0.],
       [ 2.,  1.,  0.]])

您可以从 ray_arr 与列表中选择。但是追加需要创建通过 np.concatenate 新阵列(可能是隐藏在 np.append 功能)。没有就地追加为列表上。

You can select from ray_arr as with the list. But appending requires creating a new array via np.concatenate (possibly hidden in the np.append function). No 'in-place' append as on a list.

所有组件阵列上的最后一个维度的最后一个元素,通过索引的高效选择。

Efficient selection of the last elements of all component arrays, by indexing on the last dimension.

In [762]: ray_arr[:,:,-1]
Out[762]: 
array([[ 2.,  1.],
       [ 0.,  0.]])

要从列表中得到相应的值射线你有一个清单COM prehension(或其他循环):

To get the corresponding values from the list rays you have to a list comprehension (or other loop):

In [765]: [r[:,-1] for r in rays]
Out[765]: [array([ 2.,  1.]), array([ 0.,  0.])]

有没有索引的快捷与数组。

There's no indexing shortcut as with arrays.

有像拉链工具(和其他人在和itertools ),帮助您通过列表迭代,甚至重新排列值,如:

There are tools like zip (and others in itertools) that help you iterate through lists, and even rearrange values, e.g.

In [773]: list(zip(['a','b'],['c','d']))
Out[773]: [('a', 'c'), ('b', 'd')]
In [774]: list(zip(['a','b'],['c','d']))[-1]
Out[774]: ('b', 'd')

和衣衫褴褛的子列表:

and with ragged sublists:

In [782]: list(zip(['a','b','c'],['d']))
Out[782]: [('a', 'd')]
In [783]: list(itertools.zip_longest(['a','b','c'],['d']))
Out[783]: [('a', 'd'), ('b', None), ('c', None)]

但我看不出那些将与从光向量提取值的帮助。

But I don't see how those will help with extracting values from your ray vectors.

东西值得探讨的是收集基向量成一个二维数组,并使用索引来额外的群体出于各种目的,

Something worth exploring is to collect the base vectors into one 2d array, and use indexing to extra groups for various purposes,

In [867]: allrays=np.array([r1,r2,r3,r4])
In [868]: allrays
Out[868]: 
array([[ 0.,  1.,  2.],
       [ 1.,  1.,  1.],
       [ 0.,  0.,  0.],
       [ 2.,  1.,  0.]])

在'Z'COOR所有光线

The 'z' coor for all rays

In [869]: allrays[:,-1]
Out[869]: array([ 2.,  1.,  0.,  0.])

射线的一个子集(因为它是一个片是一视图)

One subset of rays (since it is a slice it is a view)

In [871]: allrays[0:2,:]
Out[871]: 
array([[ 0.,  1.,  2.],
       [ 1.,  1.,  1.]])

另外一个子集:

In [872]: allrays[2:,:]
Out[872]: 
array([[ 0.,  0.,  0.],
       [ 2.,  1.,  0.]])

3项子集,与列表中选择 - 这是一份

3 item subset, selected with a list - this is a copy

In [873]: allrays[[0,1,2],:]
Out[873]: 
array([[ 0.,  1.,  2.],
       [ 1.,  1.,  1.],
       [ 0.,  0.,  0.]])
In [874]: allrays[[3],:]
Out[874]: array([[ 2.,  1.,  0.]])

通过索引获得几个子集:

several subsets obtained by indexing:

In [875]: ind=[[0,1,2],[3]]
In [876]: [allrays[i] for i in ind]
Out[876]: 
[array([[ 0.,  1.,  2.],
        [ 1.,  1.,  1.],
        [ 0.,  0.,  0.]]), 
 array([[ 2.,  1.,  0.]])]

如果该组是连续的,你可以使用拆分

If the groups are contiguous, you can use split:

In [884]: np.split(allrays,[3])
Out[884]: 
[array([[ 0.,  1.,  2.],
        [ 1.,  1.,  1.],
        [ 0.,  0.,  0.]]), array([[ 2.,  1.,  0.]])]

该子阵列的意见(检查与 .__ array_interface __ 属性。

它,实际上,只要将衣衫褴褛列出了问题上升了一个层次。尽管如此,有更多的灵活性。你可以构建其他索引子列表,例如

It does, in effect, just move the ragged list problem up a level. Still, there is more flexibility. You could construct other indexing sublists, e.g.

In [877]: ind1=[i[-1] for i in ind]   # last of all groups
In [878]: ind1
Out[878]: [2, 3]
In [879]: ind2=[i[0] for i in ind]   # first of all groups
In [880]: ind2
Out[880]: [0, 3]

您可以到 allrays 串连了一些新的价值观。然后,您可能必须重建索引列表。但我怀疑这种建筑是做了一个,而访问是重复的。

You could concatenate some new values on to allrays. You may then have to rebuild the indexing lists. But I suspect this sort of building is done one, while access is repeated.

这是有关 plt.pcolormesh 从生产 IMG 访问值(和<$ C $更早SO问题C> plt.pcolor )浮现在脑海。一保持着一个图像作为对二维网格,另外,更普遍的表面上,只是四边形的集合,每一个颜色和路径定义其界限。

An earlier SO question about accessing values from the img produced by plt.pcolormesh (and plt.pcolor) comes to mind. One maintains an image as a surface on the 2d mesh, the other, more general, is just a collection of quadrilaterals, each with a color and path defining its boundary.

这篇关于追加/提取列表元素的有效途径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆