Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

one element is missing while using Set( generator )

In the code:

import itertools
Q = [[factor(5),factor(13)],[factor(9),factor(7),factor(13)]]
print( sorted( Set( [lcm(*qq) for qq in itertools.product(*Q)] ) ) )
print( sorted( Set( (lcm(*qq) for qq in itertools.product(*Q)) ) ) )

first print produces

[13, 5 * 7, 3^2 * 5, 5 * 13, 7 * 13, 3^2 * 13]

while second print produces

[13, 5 * 7, 5 * 13, 7 * 13, 3^2 * 13]

Is this a bug?

one element is missing while using Set( generator )

In the code:

import itertools
Q = [[factor(5),factor(13)],[factor(9),factor(7),factor(13)]]
print( sorted( Set( [lcm(*qq) for qq in itertools.product(*Q)] ) ) )
print( sorted( Set( (lcm(*qq) for qq in itertools.product(*Q)) ) ) )

first print producesproduces the expected result:

[13, 5 * 7, 3^2 * 5, 5 * 13, 7 * 13, 3^2 * 13]

while second print producesproduces a list with one element (3^2 * 5) missing:

[13, 5 * 7, 5 * 13, 7 * 13, 3^2 * 13]

Is this a bug?

one element is missing while using Set( generator )

In the code:

import itertools
Q = [[factor(5),factor(13)],[factor(9),factor(7),factor(13)]]
print( sorted( Set( [lcm(*qq) for qq in itertools.product(*Q)] ) ) )
print( sorted( Set( (lcm(*qq) for qq in itertools.product(*Q)) ) ) )

first print produces the expected result:

[13, 5 * 7, 3^2 * 5, 5 * 13, 7 * 13, 3^2 * 13]

while second print produces a list with one element (3^2 * 5) missing:

[13, 5 * 7, 5 * 13, 7 * 13, 3^2 * 13]

Is this a bug?


ADDED. More weirdness - the following code

Q = [[factor(3), factor(9)], [factor(9), factor(7)]]
print( Set( [lcm(*qq) for qq in itertools.product(*Q)] ) )

produces a set with two equal elements:

Set of elements of [3^2, 3 * 7, 3^2, 3^2 * 7]

It seems that these issues are related to the fact that Factorization objects are not hashable, but why then does Set allow to create sets of them with all kinds of side effects?