Disclaimer: I'm teaching myself Python, so there might be some trivial solution to each of my questions. Patience is appreciated!
I understand that the title is a little unclear, so I'll try to clarify with an example.
Suppose we have an array of transactions:
txArray=[[u'1'],[u'2'],[u'2', u'3']]
The goal is to write a function myIntersection(arrayOfLists)
that first calculates the intersection of each possible pair of lists in txArray
, and then takes the union.
So myIntersection(txArray)
should return [u'2']
, because:
int1=intersection([u'1'],[u'2'])=[]
int2=intersection([u'1'],[u'2', u'3'])=[]
int3=intersection([u'2'],[u'2', u'3'])=[u'2']
union=(int1 U int2 U int3)=[u'2']
What I've tried so far is below:
from itertools import combinations
'''
Pseudocode:
1) Generate all possible 2-combinations of the lists in txArray
2) Flatten the lists
3) If a value appears more than once in a 2-combination, add it to
list of intersections
4) Keep unique elements in list of intersections
'''
def myIntersection(arrayOfLists):
flat_list=[]
intersections=[]
combs=list(combinations(txArray,2))
for i in range(0, len(combs)):
flat_list.append([item for sublist in combs[i] for item in sublist])
for list in flat_list:
for element in list:
if list.count(element)>1:
if element not in intersections:
intersections.append(element)
return intersections
While it works in a python command-line interface, I keep getting errors with this approach when I save it as a python file and run it.
My questions are: 1) Why doesn't it work when I run it as a python file?
2) Is there a cleaner, more 'pythonic' way to do this (possibly with list comprehensions)
3) I did think about using sets instead, but I couldn't figure out to iteratively convert the lists of arrayofLists (in the general case) to sets. Is there a simple syntax for doing that?
Thank you so much!