-3

我正在尝试对下面的 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]列表与匹配的列表连接起来,并用 ':' 分隔。因此,对于这种情况,基本上只有+和+之和与其他列表不同的列表才会在 newList 中列出。myList[i][0]myList[i][7]myList[i][3]myList[i][4]myList[i][5]myList[i][6]myListmyList[i][3]myList[i][4]myList[i][5]myList[i][6]

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

4

2 回答 2

0

我试着写。

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]
# ]
于 2014-06-09T16:25:04.787 回答
0

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

现在,在我看来,我们可以按 key 对这个列表进行排序(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]

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

于 2014-06-09T16:12:08.453 回答