1

我有一个包含以下列表的列表:

rowdata = [['100004100', 'ABC1234AS', '45.96'],
           ['100004101', 'ABC1234AS', '104.95'],
           ['100004103', '453SDFAS', '24.52']]

我试图弄清楚如果第二个值匹配,如何将第一个值和第三个值作为一个组返回。

第一个值是订单号,第二个值是批号,第三个是美元金额。我的信用卡处理器将随机订单批量处理在一起。我正在尝试查找批号匹配的行并返回这些行的订单号和金额。

我在想类似的事情,

for item in rowdata:
    if item[1] is a duplicate:
        print item[0], item[1]
4

4 回答 4

3

Python 有一些非常有用的内置工具,但有时如果您不知道要查找什么,它们可能很难找到。在这里groupby并且itemgetter很有帮助。

from itertools import groupby
from operator import itemgetter

rowdata = [['100004100', 'ABC1234AS', '45.96'],
           ['100004101', 'ABC1234AS', '104.95'],
           ['100004103', '453SDFAS', '24.52']]

for key, group in groupby(rowdata, itemgetter(1)):
    print key, list(group)

给你:

ABC1234AS [['100004100', 'ABC1234AS', '45.96'], ['100004101','ABC1234AS', '104.95']]

453SDFAS [['100004103', '453SDFAS', '24.52']]

于 2013-10-04T07:49:12.233 回答
1
rowdata = [['100004100', 'ABC1234AS', '45.96'],
           ['100004101', 'ABC1234AS', '104.95'],
           ['100004103', '453SDFAS', '24.52']]

def find_duplicates(duplicate):
    return [(item[0], item[2]) for item in rowdata if item[1] == duplicate]

print find_duplicates('ABC1234AS')

印刷:

[('100004100', '45.96'), ('100004101', '104.95')]
于 2013-10-04T07:44:13.640 回答
0

像下面这样的东西?

rowdata = [['100004100', 'ABC1234AS', '45.96'], ['100004101', 'ABC1234AS', '104.95'], ['100004103','453SDFAS', '24.52']]

bucket = defaultdict(list)
for col1, col2, col3 in rowdata:
    bucket[col2].append((col1,col3))

duplicates = {key:value for key, value in bucket.items() if len(value) > 1}

print (duplicates)

打印以下内容:

{'ABC1234AS': [('100004100', '45.96'), ('100004101', '104.95')]}

工作示例:http: //ideone.com/RBJjh5

于 2013-10-04T07:52:15.813 回答
0

如果您尝试按批号对订单进行分组,您可以使用defaultdict.

rowdata = [['100004100', 'ABC1234AS', '45.96'],
           ['100004101', 'ABC1234AS', '104.95'],
           ['100004103', '453SDFAS', '24.52']]

batches = defaultdict(list)

for order in rowdata:
    batches[order[1]].append([order[0], order[2]])

for batch, orders in batches.iteritems():
    print "%s: %s" % (batch, orders)

输出:

ABC1234AS: [['100004100', '45.96'], ['100004101', '104.95']]
453SDFAS: [['100004103', '24.52']]
于 2013-10-04T07:45:27.710 回答