Created
December 22, 2011 21:55
-
-
Save myano/1512026 to your computer and use it in GitHub Desktop.
check if a list of several lists have the same elements but in any order, essentially the lists should be identical except for order. (number of elements in each should be the same)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def lists_same(lol): | |
""" helper code that takes a list of list (lol) and checks to make sure | |
that all elements in each list are in all of the lists (order doesn't | |
matter) """ | |
if not isinstance(lol, list) or not len(lol) > 0: | |
return 1 | |
## lol is a list of lists | |
length = 0 | |
for each_list in lol: | |
length += len(each_list) | |
## Each element in the list are not the same size | |
if (length % len(lol)) != 0: | |
return False | |
#temp = lol[0] | |
temp = dict() | |
for each_list in lol: | |
each_list_str = str(id(each_list)) | |
temp[each_list_str] = dict() | |
for each_item in each_list: | |
if each_item in temp[each_list_str]: | |
temp[each_list_str][each_item] += 1 | |
else: | |
temp[each_list_str][each_item] = 1 | |
a = temp.keys()[0] | |
temp_dict = temp[a] | |
for ns in temp: | |
if not temp[ns] == temp_dict: | |
return False | |
temp_dict = temp[ns] | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment