How to create the union of many sets using a generator expression?
Just use the .union() method. >>> l = [set([1,2,3]), set([4,5,6]), set([1,4,9])] >>> frozenset().union(*l) frozenset([1, 2, 3, 4, 5, 6, 9]) This works for any iterable of iterables.
Just use the .union() method. >>> l = [set([1,2,3]), set([4,5,6]), set([1,4,9])] >>> frozenset().union(*l) frozenset([1, 2, 3, 4, 5, 6, 9]) This works for any iterable of iterables.
This is a problem that is pretty common: generating a relation on the fly without creating a table. SQL solutions for this problem are pretty awkward. One example using a derived table: SELECT n.id FROM (SELECT 2 AS id UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7) AS … Read more
Use set.pop: >>> {1}.pop() 1 >>> In your case, it would be: return S.pop() Note however that this will remove the item from the set. If this is undesirable, you can use min|max: return min(S) # ‘max’ would also work here Demo: >>> S = {1} >>> min(S) 1 >>> S set([1]) >>> max(S) 1 … Read more
There is a shuffle method in the List class. The methods shuffles the list in place. You can call it without an argument or provide a random number generator instance: var list = [‘a’, ‘b’, ‘c’, ‘d’]; list.shuffle(); print(‘$list’); The collection package comes with a shuffle function/extension that also supports specifying a sub range to … Read more
You should add the option onDelete=”SET NULL” in the annotation of your entity Publication like this: class Publication { /** * @ORM\ManyToOne(targetEntity=”Teacher”, inversedBy=”publications”) * @ORM\JoinColumn(name=”teacher_id”, referencedColumnName=”id”, onDelete=”SET NULL”) */ protected $teacher; } This modification is registered in the table declaration itself. Hence, you need a database migration or a schema change for this to take … Read more
HashSet consumes about 5.5 times more memory than ArrayList for the same number of elements (although they’re both still linear), and has significantly slower iteration (albeit with the same asymptotics); a quick Google search suggests a 2-3x slowdown for HashSet iteration versus ArrayList. If you don’t care about uniqueness or the performance of contains, then … Read more
A HashSet does not guarantee any order of its elements. If you need this guarantee, consider using a TreeSet to hold your elements. However if you just need your elements sorted for this one occurrence, then just temporarily create a List and sort that: Set<?> yourHashSet = new HashSet<>(); … List<?> sortedList = new ArrayList<>(yourHashSet); … Read more
It is already a list: >>> type(my_set) <class ‘list’> Do you want something like: >>> my_set = set([1, 2, 3, 4]) >>> my_list = list(my_set) >>> print(my_list) [1, 2, 3, 4] EDIT: Output of your last comment: >>> my_list = [1,2,3,4] >>> my_set = set(my_list) >>> my_new_list = list(my_set) >>> print(my_new_list) [1, 2, 3, 4] … Read more
The problem is that your Element class has not overridden the equals and hashCode methods or these implementations are broken. From Object#equals method javadoc: The equals method implements an equivalence relation on non-null object references: It is reflexive: for any non-null reference value x, x.equals(x) should return true. It is symmetric: for any non-null reference … Read more
The reason the set iteration order changes from run-to-run appears to be because Python uses hash seed randomization by default. (See command option -R.) Thus set iteration is not only arbitrary (because of hashing), but also non-deterministic (because of the random seed). You can override the random seed with a fixed value by setting the … Read more