优化子列表(在Python中)的子列表列表的串联?

时间:2015-04-21 14:27:15

标签: list python-2.7 merge coordinates sublist

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

输入列表 - 网格(下方)......

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

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

1 个答案:

答案 0 :(得分:1)

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

您还可以通过在循环前执行if然后使用time的情况来消除time==1循环中的剩余range(2,timeBlock+1)