Python 2.7列表操作功能列表

时间:2014-06-09 15:21:21

标签: python list list-manipulation

我正在尝试在下面的列表的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的规则。如果列表中的值满足以下条件,则在newList列表列表中创建新列表:

  1. myList[i][0]myList[i][7]上匹配的多个列表,但有myList[i][3]myList[i][4]的总和({1}}和myList[i][5]的总和彼此不同的myList[i][6]只是在newList
  2. 中列出
  3. 如果myList[i][0]上的多个列表匹配(这是类型)和myList[i][7](这是日期)相同,则为每组列表创建一个新列表,并使用数学{{1和myList[i][0]有{1} myList[i][7]myList[i][3]的和,以及myList[i][4]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]的总和不同于其他列表的列表newList。
  4. 上面的newList说明了我想要实现的这些结果。如果有人有任何想法如何做到这一点,他们将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:0)

因此,完全不清楚你要做什么。但我认为这应该有助于你实现它。

现在,在我看来,我们可以在密钥(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]

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

答案 1 :(得分: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]
# ]