Problem:
Here's a list as an input [A,B,C,E]
Output:
[A,B]
[A,C]
[A,E]
[B,C]
[B,E]
[C,E]
Basically, I want to take the union of the list with itself.
Here's the code:
pageSet_list = ['A', 'B', 'C', 'E']
pageSet_set = list() # To create a list of sets so that we can take their union
for page in pageSet_list:
toBeAdded = set(page)
pageSet_set.append(toBeAdded)
pageSet_list = list()
for i in range(len(pageSet_set)):
for j in range(i,len(pageSet_set)):
z=pageSet_set[i].union(pageSet_set[j])
if (len(z) == 2): # somethin = set([])
print 'z: ',z
pageSet_list.append(z)
Output of this code:
z: set(['A', 'B'])
z: set(['A', 'C'])
z: set(['A', 'E'])
z: set(['C', 'B'])
z: set(['B', 'E'])
z: set(['C', 'E'])
Error:
z: set(['C', 'B'])
Output should have been z: set(['B', 'C'])
.
Now recently, I got to know that set is an an unordered data structure and therefore it does not maintain any particular order of the elements. Performing union operation of two sets, by using union
method, changes the order of elements.
My question is:
How do I perform the set operations, union and intersection, without using data type set()?