-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 を作成するための規則。リスト内の値が次の条件を満たしている場合、リストの newList リストに新しいリストを作成します。

  1. myList[i][0]andに一致myList[i][7]するが (1) and の合計myList[i][3]myList[i][4](2) and の合計myList[i][5]myList[i][6]互いに異なる複数のリストは、newList にそのままリストされます。
  2. myList[i][0](これはタイプ) と(これは日付) の両方で一致する複数のリストが同じ場合、 (1) と の合計、および (2) と の合計を持つリストのセットごとmyList[i][7]に新しいリストを作成します。これは、他のリストとは異なります。また、一致するすべてのリストと、一致する + と + の合計を含むリストを「:」で区切って連結しようとしています。したがって、基本的にこの場合、 +と+の合計を持つリストのみが他のリストとは異なり、newList にリストされます。myList[i][0]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]myListmyList[i][3]myList[i][4]myList[i][5]myList[i][6]

上記の newList は、私が達成しようとしているこれらの結果を示しています。誰かがこれを行う方法を知っているなら、彼らは大歓迎です。ありがとうございました!

4

2 に答える 2

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]

注:前提が成り立つと仮定すると (隣り合って簡単に並べ替えることができる要素のみをマージしている)、オブジェクトをマージしないように条件を簡単に修正し、新しいマージされたオブジェクトを作成するように条件を簡単に修正できます。

于 2014-06-09T16:12:08.453 に答える
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 に答える