如何检查多个列表是否具有"maximum"数量的共享元素并返回该列表



除了引用列表之外,我还有一个由不同长度的列表组成的列表,我希望最终能够在这些列表中选择一个具有最大数量的"共享元素"的列表。使用那个引用列表并返回那个列表。有没有更好的pythonic方法?

我的数据结构和代码尝试如下:

reference_list =[(0, 0),(3、4),(5),(6 6)、(7)、(8)、(9,9)、(10,10),(15、19),(15 20)]

list_1 = [(7, 7), (8, 8), (9, 9), (10, 10)]
list_2 = [(6, 6), (7, 7), (8, 8), (9, 9), (10, 10), (15, 19), (15, 20)]
list_3 = [(0, 0), (7, 7), (10, 10), (15, 19), (15, 20)]
list_4 = [(2, 2), (8, 8), (9, 9), (7, 7), (8, 8), (9, 9)]
#list_5 = [(5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10), (15, 19), (15, 20)]
#list_6 = [(0, 0), (3, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10), (15, 19), (15, 20)]
list_of_lists = (list_1, list_2, list_3, list_4)#, list_5)#, list_6)

counts = []
#flag = 0
for list_element in list_of_lists:
count = 0
print(list_element)
lengh_of_element = len(list_element)
for i in range(lengh_of_element):
if list_element[i] in reference_list:
count=count+1
counts.append(count)
maximum_count = max(counts)
max_count_index = counts.index(maximum_count)
selected_list = list_of_lists[max_count_index]
print('the list with maximum number of shared elements with the reference list is: list_', max_count_index+1)

你可以直接做

s = pd.DataFrame(list_of_lists).isin(reference_list).sum(1)
Out[351]: 
0    4
1    7
2    5
3    5
dtype: int64
list_of_lists[s.idxmax()]
Out[352]: [(6, 6), (7, 7), (8, 8), (9, 9), (10, 10), (15, 19), (15, 20)]

最新更新