我正在使用python。我有一个名为QuerySet
的坐标列表,我想将其翻译成另一个系统,如下所示:
segments_init
注意:每个segments_init = [x1, y1, z1, x2, y2, z2, x3, y3, z3, ...]
segments_new = [x1, -z1, 0, x2, -z2, 0, x3, -z3, 0, ...]
列表由软件内置命令生成,如下所示
segments_init
答案 0 :(得分:0)
>>> segments_init = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(zip(segments_init[0::3], list(map(lambda x:-x, segments_init[2::3])), [0]*(len(segments_init)//3)))
[(1, -3, 0), (4, -6, 0), (7, -9, 0)]
尽管最好将单个3D坐标放在一起以提高可读性,但如果你想要整理列表,你可以使用它:
>>> [i for l in list(zip(segments_init[0::3], list(map(lambda x:-x, segments_init[2::3])), [0]*(len(segments_init)//3))) for i in l]
[1, -3, 0, 4, -6, 0, 7, -9, 0]
答案 1 :(得分:0)
使用单行列表理解:
>>> segments_init = [1,2,3,4,5,6,7,8,9]
>>>> [j if i%3 == 0 else -segments_init[i+1] if i%3 == 1 else 0 for i, j in enumerate(segments_init)]
[1, -3, 0, 4, -6, 0, 7, -9, 0]
或者,使用extended slicing:
>>> segments_init = [1,2,3,4,5,6,7,8,9]
>>> segments_new = list(segments_init)
>>> segments_new[1::3] = [-i for i in segments_init[2::3]]
>>> segments_new[2::3] = len(segments_init)//3 * [0]
>>> segments_new
[1, -3, 0, 4, -6, 0, 7, -9, 0]