2015-04-01 170 views
-1

我要比較兩個組,並找到在蟒蛇的區別:蟒蛇比較的元組和字典

>>> mysql_orders = ((50434L, 5901L), (50733L, 5901L)) 
>>> opera_orders = [{'orderId': 'WEB050434', 'accountId': '00T001'}, {'orderId': 'WEB050733', 'accountId': '00T001'}, {'orderId': 'DOC075185', 'accountId': '00T001'}, {'orderId': 'WEB081859', 'accountId': '00T001'}] 

一個是元組列表和字典其中列表中的第一個項目可能是另外一個列表沒有WEB/DOC前綴的orederId

找不到mysql_orders列表中缺失的orderId的正確方法是什麼?

+2

絕對零功夫。嘗試自己編寫代碼並將其發佈到您的問題中。 – smci 2015-04-01 10:07:47

+1

也許爲你的示例數據包含所需的函數輸出會有幫助嗎? – 2015-04-01 10:08:08

+0

您的訂單ID前綴爲「WEB/DOC」或「WEB0/DOC0」?而你的mysql_order ids似乎是Long類型的。那麼你會如何比較呢? – 2015-04-01 10:09:48

回答

0

使用正則表達式去除字母,但前提是數字總是晚一些,

import re 

mysql_orders = ((50434L, 5901L), (50733L, 5901L)) 

opera_orders = [{'orderId': 'WEB050434', 'accountId': '00T001'}, {'orderId': 'WEB050733', 'accountId': '00T001'}, {'orderId': 'DOC075185', 'accountId': '00T001'}, {'orderId': 'WEB081859', 'accountId': '00T001'}] 

mysql_orders = [element for tupl in mysql_orders for element in tupl] 

missing_order_ids = [opera_order['orderId'] for opera_order in opera_orders if long(re.split('\D+', opera_order['orderId'])[1]) not in mysql_orders] 

print missing_order_ids 
0

如果信件可能會或可能不會有,但領先的0是永遠存在的,你可以用一組帶:

from string import ascii_letters 

st = set(str(a) for a,_ in mysql_orders) 

missing = [d['orderId'] for d in opera_orders if not d['orderId'].strip(ascii_letters)[1:] in st] 

print(missing) 

['DOC075185', 'WEB081859'] 

如果前綴是永遠存在的只是切片:

missing = [d['orderId'] for d in opera_orders if not d['orderId'][4:] in st]