【问题标题】:Python matching-item based concatenation of list of sublists?基于 Python 匹配项的子列表连接?
【发布时间】:2015-05-11 14:32:27
【问题描述】:

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

[几何,名称,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)

电流输出:

上述 InputMerging 方法 给出以下结果,以突出显示理想情况下的内容和方式合并。

['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', '', '', '', '', '', '', '', '', '', '', '']

结论性问题:

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

例如,下面每个子列表的 key-matching 索引将是 [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', '', '', '', '', '', '', '', '', '', '', '']

【问题讨论】:

  • 你能解释一下你要做什么吗?
  • 有什么线索吗? (关于你想要什么,即这是否有效,如果没有,那是实际输出还是所需输出,以及另一个是什么)
  • 致最初的 cmets:编辑原始帖子以反映您的(好的)建议。
  • 这篇文章没有任何问题。标题有一个问号,但也不是一个问题。您可能想解释一下您正在尝试做什么,以及您遇到了什么问题。
  • @Emile:谢谢,根据您的建议更新。

标签: python list merge sublist


【解决方案1】:

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

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

【讨论】:

  • 一旦你习惯了它,它就是一个非常有用的工具。能够以您想要的方式将任意数量的可迭代项串在一起非常有用。
  • 失去了网络访问权限并提出了列出的解决方案 - 不像您提供的链接那么优雅(还),但也许我可以采用这种方式。智慧赞赏。
【解决方案2】:

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

当前工作解决方案:

        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', '', '', '', '', '', '', '', '', '', '', '']]

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多