Python2.7列表操作功能

2024-05-16 08:52:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试对下面的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][0]myList[i][7]myList[i][5]myList[i][6]的和。我还尝试将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][6],然后在newList中列出。你知道吗

上面的新列表说明了我正在努力实现的这些结果。如果有人有任何想法如何做到这一点,他们将不胜感激。谢谢您!你知道吗


Tags: id列表forissuersttradesbreakvalidity
2条回答

我试着写作。你知道吗

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]
# ]

所以,完全不清楚你想做什么。但我认为这会帮助你完成它。你知道吗

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

注意:假设前提成立(您只合并可以轻松排序的元素),您可以非常轻松地修复不合并对象的条件,也可以非常轻松地修复创建新合并对象的条件。你知道吗

相关问题 更多 >