基于Z值~Python的分裂矢量三维阵列

时间:2015-11-17 03:40:14

标签: python

我正在制作C4D插件并且想知道:如何使用groupby根据包含的值拆分数组?

我将此代码发送到splinebuilder函数,但如果我按原样发送样条列表,我会在行之间建立连接,这是不想要的。

enter image description here

所以我需要发送单独的列表,以便 splinebuilder 创建这些行的集合,避免互连。

enter image description here

#initial contains XYZ vectors

splinelist = [(-200, 0, -200), (0, 0, -200), (200, 0, -200), (-200, 0, 0), (0, 0, 0), (200, 0, 0), (-200, 0, 200), (0, 0, 200), (200, 0, 200)]


#desired handcoded for example separated based on Z value

la = [(-200, 0, -200), (0, 0, -200), (200, 0, -200)]
lb = [(-200, 0, 0), (0, 0, 0), (200, 0, 0)]
lc = [(-200, 0, 200), (0, 0, 200), (200, 0, 200)]


#desired list of lists based on Z value

desiredlist = [[(-200, 0, -200), (0, 0, -200), (200, 0, -200)],[(-200, 0, 0), (0, 0, 0), (200, 0, 0)],[(-200, 0, 200), (0, 0, 200), (200, 0, 200)]]

1 个答案:

答案 0 :(得分:1)

我认为这可以满足您的需求:

import itertools
splinelist = [(-200, 0, -200), (0, 0, -200), (200, 0, -200), (-200, 0, 0), (0, 0, 0), (200, 0, 0), (-200, 0, 200), (0, 0, 200), (200, 0, 200)]
grouped = itertools.groupby(splinelist, lambda x : x[2])
desiredlist  = [list(group) for key, group in grouped]
print(desiredlist)

输出:

[[(-200, 0, -200), (0, 0, -200), (200, 0, -200)], [(-200, 0, 0), (0, 0, 0), (200, 0, 0)], [(-200, 0, 200), (0, 0, 200), (200, 0, 200)]]