Home > Mobile >  Combination of 2 itemsets is created in the other way around
Combination of 2 itemsets is created in the other way around

Time:10-10

I'm new to Python and I'm writing code to combine items. So I have for example this array

[1, 12, 13, 15, 21, 24,28, 29, 35, 36]

I'm generating my set of items this way:

for e1, e2 in combinations(array, 2):
    item = e1| e2 # union of two sets
        

so I get

[1,12] [1,13] [1,15] [1,21]

and then

[24,1]

instead of

[1,24]

and then

[1,28]

the combination is pretty normal. In all iterations i got this issue, do you know why this happens.

Any assistance would be highly appreciated.

CodePudding user response:

I am rather sure your confusion is caused by the following:

>>> {1, 12}
{1, 12}
>>> {1, 13}
{1, 13}
# ...
>>> {1, 21}
{1, 21}
>>> {1, 24}
{24, 1}  # first odd example

Well, sets are inherently unordered, so {1, 24} and {24, 1} are just two representations of exactly the same thing.

CodePudding user response:

As @schwobaseggl said, sets are unordered and if you want it to be ordered you should use sorted(item) or list(item)

  • Related