我有像蟒蛇(真正的一個是巨大的,我不僅可以通過看它這樣做)以下列表:蟒蛇傳遞
original1=[['email', 'tel', 'fecha', 'descripcion', 'categ'],
['[email protected]', '1', '2014-08-06 00:00:06', 'MySpace a', 'animales'],
['[email protected]', '1', '2014-08-01 00:00:06', 'My Space a', 'ropa'],
['[email protected]', '2', '2014-08-06 00:00:06', 'My Space b', 'electronica'],
['[email protected]', '3', '2014-08-10 00:00:06', 'Myace c', 'animales'],
['[email protected]', '4', '2014-08-10 00:00:06', 'Myace c', 'animales']]
我的數據和名稱工作之間拆分它數據:
datos=original1[-(len(original1)-1):len(original1)]
我需要做的是擁有所有的副本一起,考慮電子郵件和電話的字典,但我需要應用及物:由於線0 =行,如果我們考慮到的電子郵件2,但還行1如果我們考慮電話,並且第一行=如果我們再次考慮電子郵件的第三行,我需要坦白地說在這種情況下,ate是0,1,2和3,而4是單獨的。
我創建了下面的代碼:
from collections import defaultdict
email_to_indices = defaultdict(list)
phone_to_indices = defaultdict(list)
for idx, row in enumerate(datos):
email = row[0].lower()
phone = row[1]
email_to_indices[email].append(idx)
phone_to_indices[phone].append(idx)
所以現在我需要申請轉移性規則,扎堆0-3,獨自4
如果打印
print 'email', email_to_indices
print 'phone', phone_to_indices
您將獲得:
email defau (,{'[email protected]':[0,2],'[email protected]': [1,3],'[email protected]':[4]})
電話defaultdict({ '1':[0,1], '3':[3], '2':[2], '4':[4]})
不要知道如何獲得那些考慮傳遞性財產的人的聯合。 我需要這樣的:
first_group:0,1,2,3]
second_group:[4]
謝謝!
應該是什麼輸出字典的關鍵?它是否應該爲每個唯一的電子郵件和電話設置一個密鑰,每個電子郵件和電話都引用相同的數據列表,還是應該存在某種由所有電子郵件和數字重疊構成的合併密鑰?你的預期產出是多少? – 2014-08-27 18:16:40
在我看來,行動的自然過程將是類似於[[line0,line1,line2,line3],[line4]]的數據結構' – 2014-08-27 18:28:21
Adam,事情就是這是一個例子,真正的表是巨大的。這就是我編寫代碼的原因。 – GabyLP 2014-08-27 18:29:55