python 列表取交集

编程入门 行业动态 更新时间:2024-10-15 12:34:35
本文介绍了python 列表取交集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

问 题

a = [[1,2],[3,4]] b = [[2,3],[3,5]]

我想取 a 和 b 的交集,结果应该是 [3,4],[3,5] ,就是第一元素要一样我的笨办法是先取 a 和 b 第一个元素的列表,取它们的交集,然后再分别从 a 和 b 中找请问有没有更好的办法?

补充:对不起大家我没说清楚。a 和 b 中的元素有很多,且每一项的第一个元素时不会相同的,最后取出来是两个列表

我的代码如下:

def getItemList(dictionary,uid,vid=""): if(vid == ""): itemList = dictionary[uid] return itemList else: itemList1 = dictionary[uid] itemList2 = dictionary[vid] list1 = [] list2 = [] list3 = [] for item in itemList1: list1.append(item[0]) for item in itemList2: list2.append(item[0]) list1 = list(set(list1).intersection(set(list2))) list2.clear() for item in list1: for li in itemList1: if li[0] == item: list2.append(li) for li in itemList2: if li[0] == item: list3.append(li) return list3,list2

dictionary = {'1':[['1', '5'], ['2', '3'], ['3', '4'], ['4', '3'], ['5', '3'], ['7', '4'], ['8', '1'], ['9', '5'], ['11', '2'], ['13', '5'], ['15', '5'], ['16', '5'], ['18', '4'], ['19', '5'], ['21', '1'], ['22', '4'], ['25', '4'], ['26', '3'], ['28', '4'], ['29', '1'], ['30', '3'], ['32', '5'], ['34', '2'], ['35', '1'], ['37', '2'], ['38', '3'], ['40', '3'], ['41', '2'], ['42', '5'], ['43', '4'], ['45', '5'], ['46', '4'], ['48', '5'], ['50', '5'], ['52', '4'], ['55', '5'], ['57', '5'], ['58', '4'], ['59', '5'], ['63', '2'], ['66', '4'], ['68', '4'], ['71', '3'], ['75', '4'], ['77', '4'], ['79', '4'], ['83', '3'], ['87', '5'], ['88', '4'], ['89', '5'], ['93', '5'], ['94', '2'], ['95', '4'], ['99', '3'], ['101', '2'], ['105', '2'], ['106', '4'], ['109', '5'], ['110', '1'], ['111', '5'], ['115', '5'], ['116', '3'], ['119', '5'], ['122', '3'], ['123', '4'], ['124', '5'], ['126', '2'], ['127', '5'], ['131', '1'], ['133', '4'], ['135', '4'], ['136', '3'], ['137', '5'], ['138', '1'], ['139', '3'], ['141', '3'], ['142', '2'], ['144', '4'], ['146', '4'], ['147', '3'], ['149', '2'], ['152', '5'], ['153', '3'], ['156', '4'], ['158', '3'], ['162', '4'], ['165', '5'], ['166', '5'], ['167', '2'], ['168', '5'], ['169', '5'], ['172', '5'], ['173', '5'], ['176', '5'], ['178', '5'], ['179', '3'], ['181', '5'], ['182', '4'], ['187', '4'], ['191', '5'], ['192', '4'], ['194', '4'], ['195', '5'], ['197', '5'], ['198', '5'], ['199', '4'], ['203', '4'], ['204', '5'], ['205', '3'], ['207', '5'], ['211', '3'], ['216', '5'], ['217', '3'], ['220', '3'], ['223', '5'], ['231', '1'], ['234', '4'], ['237', '2'], ['238', '4'], ['239', '4'], ['240', '3'], ['244', '2'], ['245', '2'], ['246', '5'], ['247', '1'], ['249', '4'], ['251', '4'], ['256', '4'], ['257', '4'], ['261', '1'], ['263', '1'], ['268', '5'], ['269', '5'], ['270', '5'], ['271', '2']],'2':[['1', '4'], ['10', '2'], ['14', '4'], ['25', '4'], ['100', '5'], ['111', '4'], ['127', '5'], ['237', '4'], ['242', '5'], ['255', '4'], ['258', '3'], ['269', '4'], ['272', '5'], ['273', '4'], ['274', '3'], ['275', '5'], ['276', '4'], ['277', '4'], ['278', '3'], ['282', '4'], ['283', '5'], ['284', '4'], ['285', '5'], ['286', '4'], ['287', '3'], ['288', '3'], ['289', '3'], ['291', '3'], ['293', '4'], ['294', '1'], ['295', '4'], ['296', '3'], ['300', '4'], ['302', '5'], ['304', '4'], ['305', '3'], ['306', '4'], ['309', '1'], ['310', '4'], ['311', '5']]}

这种格式(测试数据)

其实我想改进 else 这一段的效率,我用 line_profiler 分析结果如下:

可以看出我的方法效率很差如果改成下面这样会好很多

恩,就是这样,补充完毕!

解决方案

因為還不知道你想要的細節, 就先給一個暫時的答案吧:

from collections import defaultdict a = [[1,2],[3,4],[3,6]] b = [[1,2],[2,3],[3,5]] dic_a = defaultdict(list) dic_b = defaultdict(list) for item in a: dic_a[item[0]].append(item) for item in b: dic_b[item[0]].append(item) common_keys = set(dic_a.keys()) & set(dic_b.keys()) for key in common_keys: print 'key:', key, 'a:', dic_a[key], 'b:',dic_b[key]

我回答過的問題: Python-QA

更多推荐

python 列表取交集

本文发布于:2023-11-29 12:27:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646393.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!