-11

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()?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kshikhar
  • 573
  • 1
  • 7
  • 11
  • perhaps look into the [NumPy library](https://docs.scipy.org/doc/numpy/index.html)? – Christian Dean Dec 07 '16 at 18:19
  • is the order re-creatable? could you just sort the data later on to re-establish its order? and how is order maintained through, say, intersection? if have `(1, 2, 3)` and `(3, 2)`, what's the result? – acushner Dec 07 '16 at 18:21
  • @AndreaCorbellini I have edited the question. No, it is not a duplicate. – kshikhar Dec 07 '16 at 18:51
  • @acushner I have edited the question. Please take a look. – kshikhar Dec 07 '16 at 18:52
  • 5
    So you want [combinations](https://docs.python.org/3/library/itertools.html#itertools.combinations)? I don't see why sets need to be involved here – Andrea Corbellini Dec 07 '16 at 18:53
  • @AndreaCorbellini Can this be done using lists and some simple logic? – kshikhar Dec 07 '16 at 19:10
  • And i am going through the documentation link which you have provided @AndreaCorbellini – kshikhar Dec 07 '16 at 19:13
  • Try `list(itertools.combinations(['A', 'B', 'C', 'E'], 2))` – Robᵩ Dec 07 '16 at 19:16
  • Okay it's working for input [A,B,C,E] @Robᵩ How to make this work for list [['A', 'B'], ['A', 'C'], ['A', 'E']] ? So that I get output : [[A,B,C], [A,B,E], [A,C,E]] (combinations of length 3) – kshikhar Dec 07 '16 at 19:26
  • 2
    Basically what you want is **not** taking the union of the list with itself (and has nothing to do with the `set` datatype). You're just finding all the combinations of pairs—in other words, two at a time—of the elements of a list. – martineau Dec 07 '16 at 19:40
  • 1
    I'm agreeing with the duplicate here; if you just enter `set(['B', 'C'])` into the interpreter, it will reverse the order to `set(['C', 'B'])`. – Makoto Dec 07 '16 at 20:14
  • This post is linked from [Meta](http://meta.stackoverflow.com/questions/339191/can-existing-users-be-friendly-here-to-newcomers-who-ask-genuine-questions) (10k+ link) so it may have additional downvotes on top of mismatch between what is asked (set operations) with what is expected (probably caresian product which produces ordered pairs and not sets) – Alexei Levenkov Dec 08 '16 at 07:05

1 Answers1

1
import itertools

for acom in itertools.combinations('ABCD',2):
    print acom
Organis
  • 7,243
  • 2
  • 12
  • 14
  • Okay it's working for input [A,B,C,E] . How to make this work for list [['A', 'B'], ['A', 'C'], ['A', 'E']] ? So that I get output : [[A,B,C], [A,B,E], [A,C,E]] (combinations of length 3) – kshikhar Dec 07 '16 at 19:28
  • for acom in itertools.combinations(['AB', 'AC', 'AE'] ,2): print sorted(set(acom[0] + acom[1])) – Organis Dec 07 '16 at 19:37
  • for acom in combinations(['AB', 'AC', 'AE','BC','BE','CE'] ,2): print sorted(set(acom[0] + acom[1])) How to get rid of the duplicates? – kshikhar Dec 07 '16 at 19:53
  • print map(list,list(set([''.join(sorted(set(acom[0] + acom[1]))) for acom in itertools.combinations(['AB','AC','AE','BC','BE','CE'],2)]))) – Organis Dec 07 '16 at 20:08
  • If you untangle that pythonic mumbo jumbo, it goes as following: first you get a list of combinations as strings, then remove duplicates by converting it into set, then convert it into list again, and finally dismantle strings into lists: list('ABC') is ['A','B','C']. – Organis Dec 07 '16 at 20:10
  • yy = set() for acom in combinations(['AB', 'AC', 'AE','BC','BE','CE'] ,2): yy.add(set(acom[0] + acom[1])) @organis I tried converting it to a list but i am getting an error: TypeError: unhashable type: 'set' – kshikhar Dec 07 '16 at 20:17
  • Set elements must be hashable (simply put, can be used as dictionary keys: numbers, strings, booleans). This thread explains that nicely: http://stackoverflow.com/questions/6310867/why-arent-python-sets-hashable – Organis Dec 07 '16 at 21:34
  • This thread helped a lot. thanks, @Organis – kshikhar Dec 18 '16 at 12:08