I have a question regarding comparison of multiple lists. I have a "master list" and 5 sublists. Some of the items in the 5 sublists are identical, and not all of them match the ones in the master list. I know which of these are in each, however the master list is large. This might be kind of confusing, but I need to identify the overlaps in these sublists to mark for different colors in networkx.
My code right now: (and it doesn't work)
master = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] s1 = [ 1, 2, 3] s2 = [1, 3, 4] s3 = [1, 2, 6] s4 = [2, 3, 4] colors = [] for m in masterlist: if m in s1 and m in s2 and m in s3: colors.append('magenta') elif m in s1 and m in s3 and m in s4: colors.append('blue') elif m in s1 and m in s2 and m in s4: colors.append('green') elif m in s1 and m in s3: colors.append('cyan') elif m in s2 and m in s4: colors.append('tan') elif m in s1: colors.append('aquamarine') elif m in s2: colors.append('gold') elif m in s3: colors.append('yellow') elif m in s4: colors.append('black') else: colors.append('gray') print colors Desired output:
['gray', 'magenta', 'blue', 'green', 'tan', 'gray', 'yellow', 'gray', 'gray', 'gray', 'gray'] I noticed that the points where it doesn't work are the lines with two AND statements. Does anyone know how I should change this? Should I use something like 'contains'?
I also need to know where the overlaps occur, by color. So I've been using a count method for the colors list:
print "s1-2-3 overlaps:", colors.count('magenta') print "s1-3-4 overlaps:", colors.count('blue') print "s1-2-4 overlaps:", colors.count('green') print "s1 unique:", colors.count('aquamarine') ... The output I need based on the example above is a list with color strings. If a item in the master list is contained in all 5 sublists, I need a color name to be in the same position in the color list as the master list. Then for all remaining items in the sublists, I need a different color appended to the color list for each one, with all other items in the master list not matching any sublists, to be colored the same color. Again, this is for a networkx graph. So the colors will correspond to nodes.
I will be doing this 30+ times making many graphs, so I need the colors for matches within the elif statements to remain the same, so that they are same for each. Sometimes items in sublists will match, sometimes they won't.
m in s1and such over and over, maybe evaluate each of them once, assign them to variables (m1 = m in s1), and then just test those booleans. Might not solve the problem but it should speed up your code.setfor this, specifically theintersectionmethod. Assuming of course you don't have/care about duplicates.a.intersection(b).intersection(c)will give you the elements that are ina,b, andc. Or you can pass multiple iterators tointersectionto get the same effect.