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

2024-09-28 18:45:29 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我把这段代码发送给splinebuilder函数,但是如果我按原样发送splinelist,就得到了行之间的连接,这是不需要的。你知道吗

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)]]

Tags: 函数代码插件列表valueon数组initial
1条回答
网友
1楼 · 发布于 2024-09-28 18:45:29

我想这正是你需要的:

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)]]

相关问题 更多 >