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
分析结果如下:
可以看出我的方法效率很差
如果改成下面这样会好很多
恩,就是这样,补充完毕!
因为还不知道你想要的细节, 就先给一个暂时的答案吧:
我回答过的问题: Python-QA
我的思路就是建立字典,然后直接查找
不应该是这样吗?
修改:
这样行不行
我不是来回答问题的,我看的楼主在用line_profiler,我想问一下,我刚下载了line_profiler,不知道安装好了没,我下载的源码,然后直接运行
python setup.py install
但是用的时候都会报错:NameError: name 'profile' is not defined.不知道是什么原因,应该怎么解决?