优化子列表子列表的串联 {in Python}?

Optimizing the concatenation of a list of sublists of sublists {in Python}?

下面讨论了将许多小段聚合成一条连续线的减速聚合,我想知道是否有一种基本的能力或方法是 Python 固有的,可以改善密集列表列表的压缩列出。

输入list-mesh(下)...

[[[(439126.42276230257, 4366763.865840635), (439127.8329119178, 
4366764.384558427)], [(439064.92276230257, 4366799.37288219), 
(439064.21935966297, 4366800.934456211)], [(439064.92276230257, 
4366728.35879908), (439064.1251401864, 4366727.765222029)], 
[(439127.8329119178, 4366764.384558427), (439129.18782485375, 
4366764.924527419)], [(439064.21935966297, 4366800.934456211), 
(439063.5045345232, 4366802.470752875)], [(439064.1251401864, 
4366727.765222029), (439063.3147971064, 4366727.180521704)], 
[(439129.18782485375, 4366764.924527419), (439130.4896647748, 
4366765.486618243)], [(439063.5045345232, 4366802.470752875), 
(439062.7781013942, 4366803.982181352)], [(439063.3147971064, 
4366727.180521704), (439062.4915301808, 4366726.604565358)]], 
[[(439212.46675091964, 4366676.785177457), (439212.6113935248, 
4366676.973096527), (439212.8949954961, 4366677.397581227)], 
[(439150.96675091964, 4366712.292219012), (439151.29482351645, 
4366712.568996211)], [(439150.96675091964, 4366641.278135902), 
(439151.01221407554, 4366641.631532382)], [(439212.8949954961, 
4366677.397581227), (439213.32280838164, 4366678.03858695)], 
[(439151.29482351645, 4366712.568996211), (439151.623562939, 
4366712.845224134)], [(439151.01221407554, 4366641.631532382), 
(439151.05788385356, 4366641.983286338)], [(439213.32280838164, 
4366678.03858695), (439213.75032742484, 4366678.679960683)], 
[(439151.623562939, 4366712.845224134), (439151.9529705426, 
4366713.120903871)], [(439151.05788385356, 4366641.983286338), 
(439151.10376119264, 4366642.333405403)]], [[(439700.7404694573, 
4365997.763339574), (439700.55680522305, 4365999.341595855)], 
[(439639.2404694573, 4366033.270381129), (439638.8165085041, 
4366034.984056383)], [(439639.2404694573, 4365962.25629802), 
(439638.80424761766, 4365963.619678194)], [(439700.55680522305, 
4365999.341595855), (439700.37461392384, 4366000.93244889)], 
[(439638.8165085041, 4366034.984056383), (439638.7728443413, 
4366035.160771348), (439638.49832678796, 4366036.693936106)], 
[(439638.80424761766, 4365963.619678194), (439638.3677443037, 
4365964.982072221)], [(439700.37461392384, 4366000.93244889), 
(439700.19388374704, 4366002.535999221)], [(439638.49832678796, 
4366036.693936106), (439638.1922230592, 4366038.402710422)], 
[(439638.3677443037, 4365964.982072221), (439637.9309593339, 
4365966.343480815)]]]

...通过此功能(缓慢)过程合并;其中 timeBlock 是子列表的每个子列表的匹配索引迭代器...

for groupIdx, group in enumerate(cleanPathsGrouped):
    pathsInGroup = int(groupMemberCounts[groupIdx])
    for pathIdx, path in enumerate(group):
        for time in range(1,timeBlock+1):
            if pathIdx+1>pathsInGroup:continue
            else:
                if time==1:
                    newPathGroups[groupIdx][pathIdx]+=path
                elif time>1:
                    newPathGroups[groupIdx][pathIdx]+=group[pathIdx+(pathsInGroup*(time-1))]

导致...

[[[(439126.42276230257, 4366763.865840635), (439127.8329119178, 
4366764.384558427), (439127.8329119178, 4366764.384558427), 
(439129.18782485375, 4366764.924527419), (439129.18782485375, 
4366764.924527419), (439130.4896647748, 4366765.486618243)], 
[(439064.92276230257, 4366799.37288219), (439064.21935966297, 
4366800.934456211), (439064.21935966297, 4366800.934456211), 
(439063.5045345232, 4366802.470752875), (439063.5045345232, 
4366802.470752875), (439062.7781013942, 4366803.982181352)], 
[(439064.92276230257, 4366728.35879908), (439064.1251401864, 
4366727.765222029), (439064.1251401864, 4366727.765222029), 
(439063.3147971064, 4366727.180521704), (439063.3147971064, 
4366727.180521704), (439062.4915301808, 4366726.604565358)]], 
[[(439212.46675091964, 4366676.785177457), (439212.6113935248, 
4366676.973096527), (439212.8949954961, 4366677.397581227), 
(439212.8949954961, 4366677.397581227), (439213.32280838164, 
4366678.03858695), (439213.32280838164, 4366678.03858695), 
(439213.75032742484, 4366678.679960683)], [(439150.96675091964, 
4366712.292219012), (439151.29482351645, 4366712.568996211), 
(439151.29482351645, 4366712.568996211), (439151.623562939, 
4366712.845224134), (439151.623562939, 4366712.845224134), 
(439151.9529705426, 4366713.120903871)], [(439150.96675091964, 
4366641.278135902), (439151.01221407554, 4366641.631532382), 
(439151.01221407554, 4366641.631532382), (439151.05788385356, 
4366641.983286338), (439151.05788385356, 4366641.983286338), 
(439151.10376119264, 4366642.333405403)]], [[(439700.7404694573, 
4365997.763339574), (439700.55680522305, 4365999.341595855), 
(439700.55680522305, 4365999.341595855), (439700.37461392384, 
4366000.93244889), (439700.37461392384, 4366000.93244889), 
(439700.19388374704, 4366002.535999221)], [(439639.2404694573, 
4366033.270381129), (439638.8165085041, 4366034.984056383), 
(439638.8165085041, 4366034.984056383), (439638.7728443413, 
4366035.160771348), (439638.49832678796, 4366036.693936106), 
(439638.49832678796, 4366036.693936106), (439638.1922230592, 
4366038.402710422)], [(439639.2404694573, 4365962.25629802), 
(439638.80424761766, 4365963.619678194), (439638.80424761766, 
4365963.619678194), (439638.3677443037, 4365964.982072221), 
(439638.3677443037, 4365964.982072221), (439637.9309593339, 
4365966.343480815)]]]

也许我忽略了一个可以加快进程的替代方案?

KUDOS @Scott Hunter 提供导致以下解决方案的线索:

    for groupIdx, group in enumerate(cleanPathsGrouped):
        pathsInGroup = int(groupMemberCounts[groupIdx])
        for pathIdx, path in enumerate(group):
            if pathIdx+1>pathsInGroup:continue
            newPathGroups[groupIdx][pathIdx]+=path
            for time in range(2,timeBlock+1):
                newPathGroups[groupIdx][pathIdx]+=group[pathIdx+(pathsInGroup*(time-1))]

连接时间减少了两个数量级!

您应该在 time 循环 之前处理 pathIdx+1>pathsInGroup ;如果为真,则根本不需要执行 time 循环(因为据我所知,该循环中 pathIdxpathsInGroup 都没有变化)。

你也可以通过在循环之前执行time==1的情况然后使用range(2,timeBlock+1).[=19=来消除time循环中剩余的if ]