comparing dynamic number of lists (non-equal length) for common entries in Python -


i'm trying compare lists of different number , length have been generated dynamically user input , pattern matching. haven't included matching code, should idea of i'm trying do.

following suggestions stack overflow post, i've used 'list of lists'. i've used number of queries inputted user name lists , access them.

at end of program want comparison between lists, can't head around how this. start, i'd compare list elements , find match in of lists, i'd perform other list comparisons @ later date. can't figure out how access individual lists once i'm outside of 'for query in dom_queries' loop.

i'm super stuck , woul apreciate help!!

thanks,

# set dom_count , initialise query_list dom_count = 0 dom_queries = []  # number of query domains domain_number = raw_input('how many domains want find intersects for? ') # grab query id's while dom_count < int(domain_number):  dom_count += 1  query_domain  = raw_input('domain id query ' + str(dom_count) + ': ')  dom_queries.append(query_domain)  # initialise lists query_matches list_of_lists = [] in range(len(dom_queries)):  list_of_lists.append( [] ) list_pos = 0  # matching here each dom_query, incrementing list position each query  # , put matches list query in dom_queries:  some_match = re.search(r'xyz',some_line)  list_of_lists[int(list_pos)].append(some_match.group())  list_pos += 1  # here i'm stuck!!! # compare list's generated , find list entries  # exist in each list (can number of lists different lengths).  in range (len(dom_queries)):  common = list(set(list_of_lists[i] & .... \/^.^\/  ?? 

first, simplification. can use list comprehension create empty list of lists (just bit more pythonic). also, let's make list of sets instead of list of lists.

list_of_sets = [set() in range(domain_number)] 

then can this:

common_set = set() i, s in enumerate(list_of_sets):     if == domain_number - 1:         break     common_set = common_set.update(s.intersection(list_of_sets[i+1]) 

so, start empty set , each of sets in list, find intersection next set in list (intersection: shared items between two). use update merge intersection set set of common elements. later if want manually add item common set use add method.


Comments