Python 基于匹配项的子列表列表串联?

Python matching-item based concatenation of list of sublists?

不用太深,下面的输入表示如下:

[几何、名称、z 坐标、关键区域、...]

子列表之间的任何 key-regions 匹配都会影响子列表的合并,其中 geometry 字段组合成一个字符串,并且name 字段组合成一个字符串。同时保留两个子列表的其余部分,因为它们应该匹配。

输入:

[['Aquitards~:#>0', 'Aquitard 1', 1, '2', '', '', '', '', '', '', '', '', '', '', ''],
['Aquitards~:#>2', 'Aquitard 3', 1, '2', '', '', '', '', '', '', '', '', '', '', ''],
['Aquitards~:#>2', 'Aquitard 7', 1, '4', '', '', '', '', '', '', '', '', '', '', ''],
['Aquitards~:#>0', 'Aquitard 8', 1, '4', '', '', '', '', '', '', '', '', '', '', ''], 
['Aquitards~:#>1', 'Aquitard 2', 1, '7', '', '', '', '', '', '', '', '', '', '', ''], 
['Aquitards~:#>1', 'Aquitard 9', 1, '9', '', '', '', '', '', '', '', '', '', '', '']]

当前合并方式:
下面的代码有效,但仅用于将成对的子列表合并为一个。这需要修改或重写以允许将无限数量的匹配项合并到一个子列表中。我正在为从这里去哪里而烦恼...

        matchList = []
        rawRows = []
        for idxA,rowA in enumerate(tempList):
            for idxB,rowB in enumerate(tempList):
                if idxA!=idxB:
                    if int(rowB[3])==int(rowA[3]):
                        tempRow = [rowA[0]+'}~{'+rowB[0],rowA[1]+';'+rowB[1]]
                        reverseMatchRow = [rowB[0]+'}~{'+rowA[0],rowB[1]+';'+rowA[1]]
                        tempRow.extend(rowB[2:])
                        reverseMatchRow.extend(rowB[2:])
                        if not reverseMatchRow in rawRows:
                            rawRows.append(tempRow)
                            matchList.append(rowA)
                            matchList.append(rowB)
                            continue
                    elif rowB in matchList: continue
                elif idxA==idxB:
                    if not rowB in rawRows:
                        if not rowB in matchList:
                            rawRows.append(rowB)
                        continue
        for row in rawRows:
            if not row in matchList:
                self.rows.append(row)

当前输出:

以上输入合并方式 给出以下结果以突出理想情况下合并的内容和方式。

['Aquitards~:#>0}~{Aquitards~:#>2', 'Aquitard 1;Aquitard 3', 1, '2', '', '', '', '', '', '', '', '', '', '', '']
['Aquitards~:#>2}~{Aquitards~:#>0', 'Aquitard 7;Aquitard 8', 1, '4', '', '', '', '', '', '', '', '', '', '', '']
['Aquitards~:#>1', 'Aquitard 2', 1, '7', '', '', '', '', '', '', '', '', '', '', '']
['Aquitards~:#>1', 'Aquitard 9', 1, '9', '', '', '', '', '', '', '', '', '', '', '']

结论性问题:

-如何根据匹配的索引项对所有子列表的子列表列表中的每个子列表的前两项进行字符串合并;此外删除现在合并的子列表原始源子列表,并保留任何不匹配的子列表 - 导致单个 cleaned-up 子列表列表?

例如,下面每个子列表的 键匹配 索引为 [3];

理想化输入:

    [['Aquitards~:#>0', 'Aquitard 1', 1, '2', '', '', '', '', '', '', '', '', '', '', ''],
['Aquitards~:#>2', 'Aquitard 3', 1, '2', '', '', '', '', '', '', '', '', '', '', ''],
['Aquitards~:#>3', 'Aquitard 5', 1, '4', '', '', '', '', '', '', '', '', '', '', ''],
['Aquitards~:#>4', 'Aquitard 4', 1, '2', '', '', '', '', '', '', '', '', '', '', ''],
['Aquitards~:#>2', 'Aquitard 7', 1, '4', '', '', '', '', '', '', '', '', '', '', ''],
['Aquitards~:#>0', 'Aquitard 8', 1, '4', '', '', '', '', '', '', '', '', '', '', ''], 
['Aquitards~:#>1', 'Aquitard 2', 1, '7', '', '', '', '', '', '', '', '', '', '', ''], 
['Aquitards~:#>1', 'Aquitard 9', 1, '9', '', '', '', '', '', '', '', '', '', '', '']]  

理想输出:

    ['Aquitards~:#>0}~{Aquitards~:#>2}~{Aquitards~:#>4', 'Aquitard 1;Aquitard 3;;Aquitard 5', 1, '2', '', '', '', '', '', '', '', '', '', '', '']
['Aquitards~:#>2}~{Aquitards~:#>0}~{Aquitards~:#>3', 'Aquitard 7;Aquitard 8;;Aquitard 4', 1, '4', '', '', '', '', '', '', '', '', '', '', '']
['Aquitards~:#>1', 'Aquitard 2', 1, '7', '', '', '', '', '', '', '', '', '', '', '']
['Aquitards~:#>1', 'Aquitard 9', 1, '9', '', '', '', '', '', '', '', '', '', '', '']

https://docs.python.org/2/library/itertools.html#itertools.chain

是您要开始的地方。为了让您的第一遍更容易理解和调整,我建议将子列表声明为它们自己的变量,并在 .chain() 调用之前分解您要使用的切片。以这种方式理解 'under the hood' 的用法会容易得多,直到您对它有信心为止。

在没有网络访问的过程中寻找线索-这是我想出的解决方案...

当前工作解决方案:

        inherentZones = []
        for sublist in tempList:
            keyZone = int(sublist[3])
            if not keyZone in inherentZones:
                inherentZones.append(int(sublist[3]))
        possibleZones = [[] for x in xrange(len(inherentZones))]
        for sublist in tempList: 
            placementIndex = [inherentZones.index(a) for a in inherentZones\
                              if int(a)==int(sublist[3])]
            if not len(possibleZones[placementIndex[0]])==0:
                possibleZones[placementIndex[0]][0]=\
                             possibleZones[placementIndex[0]][0]+'}${'+str(sublist[0])
                possibleZones[placementIndex[0]][1]=\
                             possibleZones[placementIndex[0]][1]+';'+str(sublist[1])
            else:
                possibleZones[placementIndex[0]]=sublist

示例输入:

[['Aquitards~:#>1', 'Aquitard 9', 1, '1', '', '', '', '', '', '', '', '', '', '', ''], 
['Aquitards~:#>2', 'Aquitard 3', 1, '2', '', '', '', '', '', '', '', '', '', '', ''], 
['Aquitards~:#>2', 'Aquitard 7', 1, '2', '', '', '', '', '', '', '', '', '', '', ''], 
['Aquitards~:#>1', 'Aquitard 2', 1, '3', '', '', '', '', '', '', '', '', '', '', ''], 
['Aquitards~:#>0', 'Aquitard 1', 1, '3', '', '', '', '', '', '', '', '', '', '', ''], 
['Aquitards~:#>0', 'Aquitard 8', 1, '3', '', '', '', '', '', '', '', '', '', '', '']]

示例输出:

[['Aquitards~:#>1', 'Aquitard 9', 1, '1', '', '', '', '', '', '', '', '', '', '', ''], 
['Aquitards~:#>2}${Aquitards~:#>2', 'Aquitard 3;Aquitard 7', 1, '2', '', '', '', '', '', '', '', '', '', '', ''],
['Aquitards~:#>1}${Aquitards~:#>0}${Aquitards~:#>0', 'Aquitard 2;Aquitard 1;Aquitard 8', 1, '3', '', '', '', '', '', '', '', '', '', '', '']]

不确定它与其他选项相比有多快(考虑到它是我必须工作的唯一解决方案)。

话虽如此,它可以根据需要将尽可能多的子列表与匹配的键合并,效果很好。也许有人看到了一些细化?