0

I am working on the python script trying to create the excel file with columns in the following way:

a only

b only

c only

a and b but not in c

b and c but not in a

c and a but not in b

a and b and c.

The output would look like a 3 group venn diagram but i need in lists,and then i can export to excel spreadsheet.

list1 = ['ab','cd','gfa','eha','tu','asb','acd','cgf','ceh','dtu','ased','ra','re','sdgfsycbjs','jcjhcbsd']
list2 = ['abx','cd','gfr','eha','tu','asb','acl','cgfta','cpah','adtu','assa','fd','as','sbddsvc','jbcbh']
list3 = ['abs','cd','gfv','eh','tu','asb','ased','cgf','ceh','adtu','assa','qw','uy','hdsjb','bcjh']
a = []
b = []
c = []
ab = []
bc = []
ca = []
abc = []
for item in list1:
        if item in list2:
            if item in list3:
                if item not in abc:
                    abc.append(item)
            else:
                ab.append(item)
        else:
            if item not in ca:
                a.append(item)

for item in list2:
    if item in list3:
        if item in list1:
            if item not in abc:
                abc.append(item)
        else:
            bc.append(item)
    else:
        if item not in ab:
            b.append(item)        

for item in list3:
    if item in list1:
        if item in list2:
            if item not in abc:
                abc.append(item)
        else:
            ca.append(item)
    else:
        if item not in bc:
            c.append(item)    
botloggy
  • 383
  • 2
  • 15
  • I edited the code. but some values are being repeated, for example: the value eha is in both a and b. and we know that a should be a only and it should not appear in a but it is being appended. – botloggy Jun 29 '18 at 01:13

1 Answers1

0

What you want are sets, not lists. They support these sort of operations very well. To make sets, either call set(xs) on a list xs, or use a set literal like {'a', 'b', ... } (as opposed to a similar list literal using square brackets).

With these sets, you can do things like

foo = {'a', 'b'}
bar = {'b', 'c', 'd'}

foo.difference(bar) # returns {'a'}
foo.union(bar) # returns {'a', 'b', 'c', 'd'}
foo.intersection(bar) # returns {'b'}
foo.symmetric_difference(bar) # returns {'a', 'c', 'd'}
...

You can also convert back to lists by calling list(foo) or similar. With these tools, you can easily build up the venn diagrams you desire. Note that all these functions return new sets, never modifying the originals.

BowlingHawk95
  • 1,518
  • 10
  • 15