我使用的是python3.5,我想知道是否有更有效的方法来实现这一点。你知道吗
list1
和list2
)。你知道吗set
。你知道吗list1
基本上是1x4“矩阵”,而list2
是1x3“矩阵”。你知道吗list1
中每个元素与list2
中每个元素的交集的长度下面是一些可以工作的示例代码,但是当我的列表长度为数千时,它会有点慢。你知道吗
有没有更快/更好的方法??你知道吗
谢谢你!你知道吗
list1 = [{1,2,3}, {4,5,6}, {1,2,9}, {4,5,10}] # 1 x 4 "matrix"
list2 = [{1,3,9}, {4,2,8}, {1,0,10}] # 1 x 3 "matrix"
myoutputmatrix = []
for aset in list1:
small_list = [len(aset & asecondset) for asecondset in list2]
myoutputmatrix .append(small_list)
myoutputmatrix # [[2, 1, 1], [0, 1, 0], [2, 1, 1], [0, 1, 1]]
目前没有回答
相关问题 更多 >
编程相关推荐