python 列表取交集
PHP中文网
PHP中文网 2017-04-18 09:16:52
0
4
447
a = [[1,2],[3,4]]
b = [[2,3],[3,5]]

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


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

我的代码如下:

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 分析结果如下:


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


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

PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(4)
刘奇

因为还不知道你想要的细节, 就先给一个暂时的答案吧:

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

Peter_Zhu

我的思路就是建立字典,然后直接查找

a = [[1,2],[3,4]]
b = [[2,3],[3,5]]
dic_a = {}
dic_b = {}
for item in a:
    if item[0] in dic_a:
        dic_a[item[0]].append(item)
    else:
        dic_a[item[0]] = item
for item in b:
    if item[0] in dic_b:
        dic_b[item[0]].append(item)
    else:
        dic_b[item[0]] = item
for key in dic_a:
    if key in dic_b:
        print dic_a[key],dic_b[key]
Ty80

不应该是这样吗?

list(set(a).intersection(set(b)))

修改:


这样行不行

a = [[1, 2], [3, 4], [2, 6]]
b = [[2, 3], [3, 5],[1,5]]
for i in itertools.product(a, b):
    if i[0][0] == i[1][0]:
        print(i)
伊谢尔伦

我不是来回答问题的,我看的楼主在用line_profiler,我想问一下,我刚下载了line_profiler,不知道安装好了没,我下载的源码,然后直接运行python setup.py install 但是用的时候都会报错:NameError: name 'profile' is not defined.不知道是什么原因,应该怎么解决?

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板