[英]fastest way to combine two huge list of tuples in python
I have two list like these: 我有两个这样的清单:
L1 = [(1, 'a'), (2, 'm'), (3, 'd') ....]
L2 = [('b', 1), ('f', 2), ('x', 1), ('w', 3), ('j', 2) ... ]
and I want to produce: 我想生产:
L3 = [('a','b'),('m','f'),('a','x'),('d','w'),('m','j'), ...]
I mean, where second element in tuple of list L1 match with first element in tuple of list L2. 我的意思是,列表L1元组中的第二个元素与列表L2元组中的第一个元素匹配。
L1 and L2 are above 12 millions of tuples. L1和L2超过1200万个元组。
I have a code that works using nested loops for comparison between 'keys' of each tuples but is fairly inefficient. 我有一个使用嵌套循环在每个元组的“键”之间进行比较的代码,但是效率很低。
What could be a good alternative to do it in the faster way ? 有什么更好的替代方法可以更快地做到这一点?
Thank you in advance. 先感谢您。
dictionaries have O(1) select/insert complexity. 字典具有O(1)选择/插入复杂度。 Use them: 使用它们:
L1_dict = dict(L1)
L3 = [(L1_dict[k], v) for v, k in L2 if k in L1_dict]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.