简体   繁体   English

基于比较在列表的 Python 列表中添加元素

[英]Adding elements within a Python list of list based on a comparison

I have a list of list that looks like this:我有一个看起来像这样的列表:

['000000000000012', 'AUD ', '      -1500000.0000', '29473550', 'TD CASH', 'Currencies', 'Unsettled Transactions', 'Unsettled']
['000000000000012', 'BRL ', '          6070.5400', '        ', 'TD CASH', 'Beginning Balance', 'Positions', 'Settled']
['000000000000012', 'MXN ', '      19524996.5400', '        ', 'TD CASH', 'Beginning Balance', 'Positions', 'Settled']
['000000000000012', 'USD ', '          9937.92', '29473153', 'TD CASH', 'Sales', 'Unsettled Transactions', 'Unsettled']
['000000000000012', 'USD ', '          9937.92', '29473155', 'TD CASH', 'Sales', 'Unsettled Transactions', 'Unsettled']
['000000000000012', 'USD ', '        100252.78', '29473080', 'TD CASH', 'Sales', 'Unsettled Transactions', 'Unsettled']
['000000000000012', 'USD ', '        105306.94', '29473142', 'TD CASH', 'Sales', 'Unsettled Transactions', 'Unsettled']

Essentially, I want to perform a logic where the current and next rows are being checked based on matching elements.本质上,我想执行一个逻辑,根据匹配元素检查当前行和下一行。 So, if currentRow[0] == nextRow[0] AND currentRow[1] == nextRow[1] THEN sum up currentRow[2] and nextRow[2].因此,如果 currentRow[0] == nextRow[0] AND currentRow[1] == nextRow[1] 那么将 currentRow[2] 和 nextRow[2] 相加。

If no match is made, then just return the row like so:如果没有匹配,则只返回该行,如下所示:

['000000000000012', 'AUD ', '      -1500000.0000']
['000000000000012', 'BRL ', '          6070.5400']
['000000000000012', 'MXN ', '      19524996.5400']
['000000000000012', 'USD ', '        225435.56'] <=== this row is summed

The list of list is already sorted so, I would imagine, this makes life easier.列表列表已经排序,所以我想,这让生活更轻松。 The resulting output can be appended into a list but formatting doesn't have to as I can always re-apply it on the outbound side.生成的 output 可以附加到列表中,但不必格式化,因为我总是可以在出站端重新应用它。 For laughs, this is what I have so far:为了笑,这是我到目前为止所拥有的:

prevAcct = None
prevCurr = None
finalSum = 0
for row in max:
    if(str(row[0]).strip() == prevAcct and str(row[1]).strip() == prevCurr):
        #print row[0]
        #print row[1]
        finalAcct = str(row[0]).strip()
        finalSum = eval(row[1]) + eval(finalSum)
    else:
        print "Setting new account and currency!"
        prevAcct = row[0]
        prevCurr = row[1]
        print "DEBUG: " + prevAcct + " and " + prevCurr

I've spent a bit of time on this and it's bugging the crap out of me.我花了一些时间在这上面,它让我很烦。 Any help would be GREATLY appreciated.任何帮助将不胜感激。

There is a function for exactly this purpose: itertools.groupby() .有一个 function 正是为了这个目的: itertools.groupby()

print [key + [sum(float(x[2]) for x in it)]
       for key, it in groupby(my_list, lambda x: x[:2])]

prints印刷

[['000000000000012', 'AUD ', -1500000.0],
 ['000000000000012', 'BRL ', 6070.54],
 ['000000000000012', 'MXN ', 19524996.539999999],
 ['000000000000012', 'USD ', 225435.56]]

(Note that I used my_list for the name of the list since your name max does not seem to be a good choice -- it shadows the built-in name max() .) (请注意,我使用my_list作为列表名称,因为您的名称max似乎不是一个好的选择——它隐藏了内置名称max() 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM