2013-10-04 80 views
1

我有一個包含列表,如列表:在Python如何選擇包含列表中的重複項的行

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

我試圖找出如何返回第一個值和第三值作爲如果第二個值匹配,則爲group。

第一個值是一個訂單號,第二個值是批號,第三個是美元金額。我的信用卡處理器一起批量隨機訂單。我試圖找到批號匹配的行,並返回這些行的訂單號和金額。

我想是這樣,

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

問題是? – khellang

回答

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')] 
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']] 
3

Python有一些非常有益的內置工具,但有時他們可能很難找到,如果你不知道要尋找什麼。這裏groupbyitemgetter很有幫助。

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 0.96 '],[' 100004101' , 'ABC1234AS', '104.95']

453SDFAS [['100004103','453SDFAS','24 .52']]

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 0.96 '),(' 100004101' , '104.95')]}

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

相關問題