【问题标题】:Python 2.7 list of lists manipulation functionality [closed]Python 2.7 列表操作功能列表 [关闭]
【发布时间】:2014-06-09 15:21:21
【问题描述】:

我正在尝试对下面的 myList 列表执行几项操作,但在弄清楚它时遇到了一些麻烦。我对 Python 很陌生。

myList = [
['Issue Id','1.Completeness for OTC','Break',3275,33,33725102303,296384802,20140107],
['Issue Id','2.Validity check1 for OTC','Break',3308,0,34021487105,0,20140107],
['Issue Id','3.Validity check2 for OTC','Break',3308,0,34021487105,0,20140107],
['Issue Id','4.Completeness for RST','Break',73376,1,8.24931E+11,44690130,20140107],
['Issue Id','5.Validity check1 for RST','Break',73377,0,8.24976E+11,0,20140107],
['Liquidity','1. OTC - Null','Break',7821,0,2.28291E+11,0,20140110],
['Liquidity','2. OTC - Unmapped','Break',7778,43,2.27712E+11,579021732.8,20140110],
['Liquidity','3. RST - Null','Break',335120,0,1.01425E+12,0,20140110],
['Liquidity','4. RST - Unmapped','Break',334608,512,1.01351E+12,735465433.1,20140110],
['Liquidity','5. RST - Valid','Break',335120,0,1.01425E+12,0,20140110],
['Issue Id','1.Completeness for OTC','Break',3292,33,32397924450,306203929,20140110],
['Issue Id','2.Validity check1 for OTC','Break',3325,0,32704128379,0,20140110],
['Issue Id','3.Validity check2 for OTC','Break',3325,0,32704128379,0,20140110],
['Issue Id','4.Completeness for RST','Break',73594,3,8.5352E+11,69614602,20140110],
['Issue Id','5.Validity check1 for RST','Break',73597,0,8.5359E+11,0,20140110],
['Unlinked Silver ID','DQ','Break',3201318,176,20000000,54974.33386,20140101],
['Missing GCI','DQ','Break',3201336,158,68000000,49351.9588,20140101],
['Missing Book','DQ','Break',3192720,8774,3001000000,2740595.484,20140101],
['Matured Trades','DQ','Break',3201006,488,1371000000,152428.8348,20140101],
['Illiquid Trades','1.Completeness Check for range','Break',43122,47,88597695671,54399061.43,20140107],
['Illiquid Trades','2.Completeness Check for non','Break',39033,0,79133622401,0,20140107]
]

我正在尝试获得以下结果,但不知道该怎么做:

newList = [
['Issue Id','1.Completeness for OTC:2.Validity check1 for OTC:3.Validity check2 for OTC','Break',3275,33,33725102303,296384802,20140107],
['Issue Id','4.Completeness for RST:5.Validity check1 for RST','Break',73376,1,8.24931E+11,44690130,20140107],
['Liquidity','1. OTC - Null','Break:2. OTC - Unmapped','Break',7821 0,2.28291E+11,0,20140110],
['Liquidity','3. RST - Null:4. RST - Unmapped:5. RST - Valid','Break',335120,0,1.01425E+12,0,20140110],
['Issue Id','1.Completeness for OTC:2.Validity check1 for OTC:3.Validity check2 for OTC','Break',3292,33,32397924450,306203929,20140110],
['Issue Id','4.Completeness for RST:5. RST - Valid','Break',73594,3,8.5352E+11,69614602,20140110],
['Unlinked Silver ID','DQ','Break',3201318,176,20000000,54974.33386,20140101],
['Missing GCI','DQ','Break',3201336,158,68000000,49351.9588,20140101],
['Missing Book','DQ Break',3192720,8774,3001000000,2740595.484,20140101],
['Matured Trades','DQ','Break',3201006,488,1371000000,152428.8348,20140101],
['Illiquid Trades','1.Completeness Check for range','Break',43122,47,88597695671,54399061.43,20140107],
['Illiquid Trades','2.Completeness Check for non','Break',39033,0,79133622401,0,20140107]
]

创建新列表的规则。如果列表中的值满足以下条件,则在 newList 列表中创建一个新列表:

  1. myList[i][0]myList[i][7] 上匹配但具有(1) myList[i][3]myList[i][4] 的总和以及(2) myList[i][5]myList[i][6] 的总和彼此不同的多个列表是刚刚在 newList 中列出
  2. 如果myList[i][0](这是类型)和myList[i][7](这是日期)上的多个列表匹配,则使用数学myList[i][0]myList[i][7]为每组列表创建一个新列表具有 (1) myList[i][3]myList[i][4] 的总和以及 (2) myList[i][5]myList[i][6] 的总和,它们与具有数学 myList[i][0]myList[i][7] 的其他列表不同。我还试图连接myList[i][1],用':'分隔所有那些匹配myList[i][0]myList[i][7]的列表以及myList[i][3]+myList[i][4]myList[i][5]+myList[i][6]的总和匹配。因此,对于这种情况,基本上只有 myList 中的列表和 myList[i][3] + myList[i][4]myList[i][5] + myList[i][5] + myList[i][6] 与其他列表不同,然后在 newList 中列出。

上面的 newList 说明了我试图达到的这些结果。如果有人有任何想法如何做到这一点,他们将不胜感激。谢谢!

【问题讨论】:

  • 我的头在试图阅读您的规范时爆炸了。
  • 您可能想要考虑定义自定义类或其他东西来组织它。如果我必须处理您的代码,我将不知道发生了什么。
  • 我的脑子也坏了,但我想你会想出一个不同的数据结构。你了解dict了吗?
  • 没有帮助您的 myList 有语法错误。 (我刚刚修好了)
  • 您可以通过点击“编辑”来编辑帖子。

标签: python list list-manipulation


【解决方案1】:

我试着写。

myList = [
['Issue Id','1.Completeness for OTC','Break',3275,33,33725102303,296384802,20140107],
['Issue Id','2.Validity check1 for OTC','Break',3308,0,34021487105,0,20140107],
['Issue Id','3.Validity check2 for OTC','Break',3308,0,34021487105,0,20140107],
['Issue Id','4.Completeness for RST','Break',73376,1,8.24931E+11,44690130,20140107],
['Issue Id','5.Validity check1 for RST','Break',73377,0,8.24976E+11,0,20140107],
['Liquidity','1. OTC - Null','Break',7821,0,2.28291E+11,0,20140110],
['Liquidity','2. OTC - Unmapped','Break',7778,43,2.27712E+11,579021732.8,20140110],
['Liquidity','3. RST - Null','Break',335120,0,1.01425E+12,0,20140110],
['Liquidity','4. RST - Unmapped','Break',334608,512,1.01351E+12,735465433.1,20140110],
['Liquidity','5. RST - Valid','Break',335120,0,1.01425E+12,0,20140110],
['Issue Id','1.Completeness for OTC','Break',3292,33,32397924450,306203929,20140110],
['Issue Id','2.Validity check1 for OTC','Break',3325,0,32704128379,0,20140110],
['Issue Id','3.Validity check2 for OTC','Break',3325,0,32704128379,0,20140110],
['Issue Id','4.Completeness for RST','Break',73594,3,8.5352E+11,69614602,20140110],
['Issue Id','5.Validity check1 for RST','Break',73597,0,8.5359E+11,0,20140110],
['Unlinked Silver ID','DQ','Break',3201318,176,20000000,54974.33386,20140101],
['Missing GCI','DQ','Break',3201336,158,68000000,49351.9588,20140101],
['Missing Book','DQ','Break',3192720,8774,3001000000,2740595.484,20140101],
['Matured Trades','DQ','Break',3201006,488,1371000000,152428.8348,20140101],
['Illiquid Trades','1.Completeness Check for range','Break',43122,47,88597695671,54399061.43,20140107],
['Illiquid Trades','2.Completeness Check for non','Break',39033,0,79133622401,0,20140107]
]



newList = [
['Issue Id','1.Completeness for OTC:2.Validity check1 for OTC:3.Validity check2 for OTC','Break',3275,33,33725102303,296384802,20140107],
['Issue Id','4.Completeness for RST:5.Validity check1 for RST','Break',73376,1,8.24931E+11,44690130,20140107],

#  ['Liquidity','1. OTC - Null','Break',7821,0,2.28291E+11,0,20140110],
#  ['Liquidity','2. OTC - Unmapped','Break',7778,43,2.27712E+11,579021732.8,20140110],
#       2.28291E+11 + 0  !=  2.27712E+11 + 579021732.8 , so  I used ceil. 
['Liquidity','1. OTC - Null','Break:2. OTC - Unmapped','Break',7821,0,2.28291E+11,0,20140110],

['Liquidity','3. RST - Null:4. RST - Unmapped:5. RST - Valid','Break',335120,0,1.01425E+12,0,20140110],
['Issue Id','1.Completeness for OTC:2.Validity check1 for OTC:3.Validity check2 for OTC','Break',3292,33,32397924450,306203929,20140110],
['Issue Id','4.Completeness for RST:5. RST - Valid','Break',73594,3,8.5352E+11,69614602,20140110],
['Unlinked Silver ID','DQ','Break',3201318,176,20000000,54974.33386,20140101],
['Missing GCI','DQ','Break',3201336,158,68000000,49351.9588,20140101],
['Missing Book','DQ','Break',3192720,8774,3001000000,2740595.484,20140101],
['Matured Trades','DQ','Break',3201006,488,1371000000,152428.8348,20140101],
['Illiquid Trades','1.Completeness Check for range','Break',43122,47,88597695671,54399061.43,20140107],
['Illiquid Trades','2.Completeness Check for non','Break',39033,0,79133622401,0,20140107]
]

import math

def create():
  index = 0
  prevKey = (myList[index][0], myList[index][7])
  sumOf3rd4th = myList[index][3] + myList[index][4]
  sumOf5th6th = myList[index][5] + myList[index][6]
  answerList = []
  answerList.append(myList[index])
  index += 1

  ceil56 = lambda x : math.ceil( x / 10000000)

  while index < len(myList):
    if prevKey == (myList[index][0], myList[index][7]) and \
        sumOf3rd4th == myList[index][3] + myList[index][4] and \
        ceil56(sumOf5th6th) == ceil56(myList[index][5] + myList[index][6]):
        # sumOf5th6th == myList[index][5] + myList[index][6]:
          answerList[-1][1] += ":" + myList[index][1]
    else:
        answerList.append(myList[index])
        prevKey = (myList[index][0], myList[index][7])
        sumOf3rd4th = myList[index][3] + myList[index][4]
        sumOf5th6th = myList[index][5] + myList[index][6]
    index += 1
  return answerList

myNewList = create()
print myNewList

# [
# ['Issue Id', '1.Completeness for OTC:2.Validity check1 for OTC:3.Validity check2 for OTC', 'Break', 3275, 33, 33725102303L, 296384802, 20140107], 
# ['Issue Id', '4.Completeness for RST:5.Validity check1 for RST', 'Break', 73376, 1, 824931000000.0, 44690130, 20140107], 
# ['Liquidity', '1. OTC - Null:2. OTC - Unmapped', 'Break', 7821, 0, 228291000000.0, 0, 20140110], 
# ['Liquidity', '3. RST - Null:4. RST - Unmapped:5. RST - Valid', 'Break', 335120, 0, 1014250000000.0, 0, 20140110],
# ['Issue Id', '1.Completeness for OTC:2.Validity check1 for OTC:3.Validity check2 for OTC', 'Break', 3292, 33, 32397924450L, 306203929, 20140110], 
# ['Issue Id', '4.Completeness for RST:5.Validity check1 for RST', 'Break', 73594, 3, 853520000000.0, 69614602, 20140110], 
# ['Unlinked Silver ID', 'DQ', 'Break', 3201318, 176, 20000000, 54974.33386, 20140101], 
# ['Missing GCI', 'DQ', 'Break', 3201336, 158, 68000000, 49351.9588, 20140101], 
# ['Missing Book', 'DQ', 'Break', 3192720, 8774, 3001000000L, 2740595.484, 20140101], 
# ['Matured Trades', 'DQ', 'Break', 3201006, 488, 1371000000, 152428.8348, 20140101], 
# ['Illiquid Trades', '1.Completeness Check for range', 'Break', 43122, 47, 88597695671L, 54399061.43, 20140107], 
# ['Illiquid Trades', '2.Completeness Check for non', 'Break', 39033, 0, 79133622401L, 0, 20140107]
# ]

【讨论】:

  • 这完美!非常感谢!
【解决方案2】:

因此,完全不清楚您要做什么。但我认为这应该可以帮助您完成它。

现在,在我看来,我们可以根据键 (elem[0], elem[7]) 对这个列表进行排序。如果我们这样做,那么我们只会合并邻居。

如果是这样,那么我们可以进行排序,然后应用归约函数:

def reduction(result, rhs):
    if len(result) == 0:
        return [rhs]
    lhs = result[-1]

    if lhs[0] != rhs[0]:
        return result + [rhs]
    if lhs[7] != rhs[7]:
        return result + [rhs]

    # Compute the new lhs object
    lhs[1] += ":" + rhs[1]
    lhs[3] += rhs[3]
    lhs[4] += rhs[4]

    # Don't append the rhs object
    return result

# Sort the list such that we are only going to be merging neighbors.
myList.sort(key=lambda x: (x[0], x[7]))

# Apply a reduction
newList = functools.reduce(reduction, myList, [])

这个归约函数不是你想要的,但那是因为我不明白你到底想要什么。特别是,这段代码产生:

['Illiquid Trades', '1.Completeness Check for range:2.Completeness Check for non', 'Break', 82155, 47, 88597695671, 54399061.43, 20140107]
['Issue Id', '1.Completeness for OTC:2.Validity check1 for OTC:3.Validity check2 for OTC:4.Completeness for RST:5.Validity check1 for RST', 'Break', 156644, 34, 33725102303, 296384802, 20140107]
['Issue Id', '1.Completeness for OTC:2.Validity check1 for OTC:3.Validity check2 for OTC:4.Completeness for RST:5.Validity check1 for RST', 'Break', 157133, 36, 32397924450, 306203929, 20140110]
['Liquidity', '1. OTC - Null:2. OTC - Unmapped:3. RST - Null:4. RST - Unmapped:5. RST - Valid', 'Break', 1020447, 555, 228291000000.0, 0, 20140110]
['Matured Trades', 'DQ', 'Break', 3201006, 488, 1371000000, 152428.8348, 20140101]
['Missing Book', 'DQ', 'Break', 3192720, 8774, 3001000000, 2740595.484, 20140101]
['Missing GCI', 'DQ', 'Break', 3201336, 158, 68000000, 49351.9588, 20140101]
['Unlinked Silver ID', 'DQ', 'Break', 3201318, 176, 20000000, 54974.33386, 20140101]

注意:假设前提成立(您只是合并可以轻松排序的元素),您可以相当容易地修复不合并对象的条件,并且相当容易地修复创建新合并对象的条件。

【讨论】:

  • 嗨,抱歉 ['Missing Book','DQ Break',3192720,8774,3001000000,2740595.484,20140101], 应该是 ['Missing Book','DQ','Break',3192720,8774,3001000000,2740595.484,20140101],] - 忘了在 'DQ','Break' 之间加上逗号。非常感谢您的帮助,我现在就试试这个。
  • 这太棒了!!!非常感谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-08
  • 2013-05-09
  • 1970-01-01
相关资源
最近更新 更多