简体   繁体   中英

How can I find an intersection among multiple lists?

I have multi arrays and I want to find an intersection between them I tried the following code.

my_lists = [['Finish', 'Purpose', 'Form', 'Series', 'Tiles Type', 'Finishing'], ['Color', 'Thickness', 'Usage/Application', 'Brand', 'Marble Type', 'Material'], ['Color', 'Brand', 'Finishing', 'Origin', 'Marble Type', 'Thickness'], ['Thickness', 'Form', 'Size', 'Series', 'Usage/Application', 'Finishing'], ['Thickness', 'Material Grade', 'Size', 'Usage/Application', 'Material'], ['Usage/Application', 'Form', 'Finishing', 'Brand', 'Material', 'Shape'], ['Application Area', 'Form', 'Finishing', 'Brand', 'Color', 'Coverage Area'], ['Usage/Application', 'Marble Type', 'Thickness', 'Brand', 'Form'], ['Unit Size (mm X mm)', 'Marble Type', 'Thickness', 'Finishing', 'Usage', 'Brand'], ['Marble Type', 'Unit Size (mm X mm)', 'Usage', 'Thickness', 'Color'], ['color'], ['Thickness', 'Size', 'Usage/Application', 'Series', 'Finish', 'Marble Type'], ['Thickness', 'Usage/Application', 'Brand', 'Color', 'Marble Type', 'Unit Size (mm X mm)'], ['Color', 'Marble Type', 'Usage'], ['Thickness', 'Size', 'Material', 'Finish', 'Packaging Size', 'Packaging Type'], ['Color', 'Material', 'Thickness', 'Usage/Application', 'Back Lit', 'Brand'], ['Material', 'Pattern', 'Shape'], ['Form', 'Application Area', 'Material', 'Thickness', 'Colour', 'Finishing'], ['Color', 'Usage/Application', 'Brand', 'Series'], ['Color', 'Material', 'Thickness', 'Usage/Application', 'Brand', 'Surface Finish'], ['Brand', 'Color', 'Usage/Application', 'Thickness', 'Size', 'Finish'], ['Form', 'Material', 'Usage', 'Marble Type', 'Thickness', 'Finishing'], ['Form', 'Color', 'Marble Type', 'Unit Size', 'Features', 'Coverage Area'], ['Usage', 'Form'], ['Finish', 'Application Area', 'Purpose', 'Thickness', 'Pattern'], ['Usage/Application', 'Finishing', 'Material', 'Brand', 'Size', 'Category Type'], ['Usage/Application', 'Size', 'Color', 'Marble Type', 'Features', 'Finishing'], ['Marble Type', 'Surface Finishing', 'Stone Form', 'Usage'], ['Brand', 'Material', 'Finish', 'Thickness', 'Size']]
print(set.intersection(*map(set,list(my_lists ))))

But I get an empty set

set()

What actually I want is to find common elements among all lists

There are no common elements among all the lists in your example - you can see that the first and second lists are completely disjoint. Hence the correct returned answer of an empty set. This operation will only find any strings that are in EACH list.

EDIT

If your goal is to find the strings that are ever repeated, I would do something like the following:

import numpy as np
my_lists = [['Finish', 'Purpose', 'Form', 'Series', 'Tiles Type', 'Finishing'], ['Color', 'Thickness', 'Usage/Application', 'Brand', 'Marble Type', 'Material'], ['Color', 'Brand', 'Finishing', 'Origin', 'Marble Type', 'Thickness'], ['Thickness', 'Form', 'Size', 'Series', 'Usage/Application', 'Finishing'], ['Thickness', 'Material Grade', 'Size', 'Usage/Application', 'Material'], ['Usage/Application', 'Form', 'Finishing', 'Brand', 'Material', 'Shape'], ['Application Area', 'Form', 'Finishing', 'Brand', 'Color', 'Coverage Area'], ['Usage/Application', 'Marble Type', 'Thickness', 'Brand', 'Form'], ['Unit Size (mm X mm)', 'Marble Type', 'Thickness', 'Finishing', 'Usage', 'Brand'], ['Marble Type', 'Unit Size (mm X mm)', 'Usage', 'Thickness', 'Color'], ['color'], ['Thickness', 'Size', 'Usage/Application', 'Series', 'Finish', 'Marble Type'], ['Thickness', 'Usage/Application', 'Brand', 'Color', 'Marble Type', 'Unit Size (mm X mm)'], ['Color', 'Marble Type', 'Usage'], ['Thickness', 'Size', 'Material', 'Finish', 'Packaging Size', 'Packaging Type'], ['Color', 'Material', 'Thickness', 'Usage/Application', 'Back Lit', 'Brand'], ['Material', 'Pattern', 'Shape'], ['Form', 'Application Area', 'Material', 'Thickness', 'Colour', 'Finishing'], ['Color', 'Usage/Application', 'Brand', 'Series'], ['Color', 'Material', 'Thickness', 'Usage/Application', 'Brand', 'Surface Finish'], ['Brand', 'Color', 'Usage/Application', 'Thickness', 'Size', 'Finish'], ['Form', 'Material', 'Usage', 'Marble Type', 'Thickness', 'Finishing'], ['Form', 'Color', 'Marble Type', 'Unit Size', 'Features', 'Coverage Area'], ['Usage', 'Form'], ['Finish', 'Application Area', 'Purpose', 'Thickness', 'Pattern'], ['Usage/Application', 'Finishing', 'Material', 'Brand', 'Size', 'Category Type'], ['Usage/Application', 'Size', 'Color', 'Marble Type', 'Features', 'Finishing'], ['Marble Type', 'Surface Finishing', 'Stone Form', 'Usage'], ['Brand', 'Material', 'Finish', 'Thickness', 'Size']]
big_list = [x for a_list in my_lists for x in a_list]
unique_strings, number_of_appearances = np.unique(big_list, return_counts=True)
index = np.flip(np.argsort(number_of_appearances))
print(unique_strings[index], number_of_appearances[index])

This flattens your list-of-lists, finds the unique strings, and sorts them by how many times they appear (most to least). The first string will be the "most found element" and any string with count of more than 1 is repeated in more than one list.

I think this could help;

from functools import reduce
reduce(numpy.intersect1d, (my_lists))

Source: https://numpy.org/doc/stable/reference/generated/numpy.intersect1d.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM