Questions tagged [set-union]

In set theory, the union (denoted by ∪) of a collection of sets is the set of all distinct elements in the collection. It is one of the fundamental operations through which sets can be combined and related to each other. In C++, set_union() constructs a sorted range consisting of all elements present in one or both sorted input ranges.

88 questions
92
votes
7 answers

Pythonic way to create union of all values contained in multiple lists

I have a list of lists: lists = [[1,4,3,2,4], [4,5]] I want to flatten this list and remove all duplicates; or, in other words, apply a set union operation: desired_result = [1, 2, 3, 4, 5] What's the easiest way to do this?
AJ.
  • 25,364
  • 15
  • 77
  • 87
43
votes
1 answer

How can I find the union on a list of sets in Python?

This is the input: x = [{1, 2, 3}, {2, 3, 4}, {3, 4, 5}] and the output should be: {1, 2, 3, 4, 5} I tried to use set().union(x) but this is the error I'm getting: Traceback (most recent call last): File "", line 1, in…
Ravit
  • 1,272
  • 3
  • 14
  • 23
34
votes
7 answers

Union of multiple sets in python

[[1, '34', '44'], [1, '40', '30', '41'], [1, '41', '40', '42'], [1, '42', '41', '43'], [1, '43', '42', '44'], [1, '44', '34', '43']] I have a list of lists. My aim is to check whether any one sublist has anything in common with other…
Tapojyoti Mandal
  • 552
  • 1
  • 4
  • 12
24
votes
3 answers

Union or intersection of Java Sets

What is the simplest way to make a union or an intersection of Sets in Java? I've seen some strange solutions to this simple problem (e.g. manually iterating the two sets).
Mahozad
  • 6,430
  • 9
  • 43
  • 70
8
votes
2 answers

Practical Use of Reversed Set Operators in Python

Is there any difference between using the reversed union or intersection operators on sets in Python? for instance, s & z corresponds to s.__and__(z) z & s corresponds to s.__rand__(z) If these are supposed to return the same thing, why have the…
MadPhysicist
  • 4,190
  • 7
  • 27
  • 80
6
votes
2 answers

Probability of the Union of Three or More Sets

Consider the following sets of probabilities (the three events are NOT mutually exclusive): 0.05625 success, 0.94375 failure 0.05625 success, 0.94375 failure 0.05625 success, 0.94375 failure How do I compute the probability of at least one event…
landroni
  • 2,702
  • 1
  • 27
  • 36
5
votes
2 answers

python merge set of fronzensets into one set

I am trying to merge sets defined in a set and this is what I have now a = frozenset([1,3,4]) b = frozenset([1,2,3,4,5]) s = set() s.add(a) s.add(b) merged = set(itertools.chain.from_iterable(s)) In practice, s may contain many frozensets. Is there…
nos
  • 16,305
  • 22
  • 81
  • 113
5
votes
3 answers

How do you efficiently find a union of a list of lists of values in haskell?

Since a code example is worth a thousand words I'll start with that: testList = [1,2,2,3,4,5] testSet = map sumMapper $ tails testList where sumMapper [] = [] sumMapper (a:b) = sumMap a b sumMap a b = map (+…
Mike H-R
  • 7,147
  • 5
  • 37
  • 58
4
votes
3 answers

How do I take the union of sets?

Is there any optimal way to get all union of n sets? This is what I have done, but it is very slow for a large number of sets: public static void main(String[] args) { List>> unionSet = new ArrayList<>(); …
payam abdy
  • 43
  • 5
4
votes
1 answer

What is the correct std::set_union code?

This site claims that set_union is equivalent to the following code: template OutputIterator set_union ( InputIterator1 first1, InputIterator1 last1, …
Frank
  • 58,417
  • 87
  • 227
  • 317
3
votes
5 answers

Union of All Intersecting Sets

Given a list of objects with multiple attributes I need to find the list of sets created by a union of all intersecting subsets. Specifically these are Person objects, each with many attributes. I need to create a list of 'master' sets based on a…
3
votes
1 answer

Why does set().union(*list1) gives me the union of two list inside a list?

I am doing an assignment, which requires me to do a code walkthrough. I would like to have a brief description on how set().union(*list1) works so i can answer during my walkthrough. list1 = [[1,2,3],[1,2,3,5,8]] x =…
3
votes
2 answers

set_union got wrong result when i called it twice

this problem confused me for many hours, please help me! the first time I call set_union, the result is right, but second I call it, the result is wrong, see the code: std::vector set1{ 1, 2, 3, 4, 5, 6 }; std::vector set2{ 4, 5, 6, 7, 8…
wdbg
  • 39
  • 4
3
votes
2 answers

Union geometries faster in JTS?

I wrote code to join (union) geometries. I wrapped it into Java8 streams Collector. Inside it it just uses Geometry#union to union geometries: geometries[0] = geometries[0].union(geometry); Unfortunately, it works rather slow. Is it possible to…
Dims
  • 37,353
  • 77
  • 251
  • 478
3
votes
2 answers

Intersections and Unions in Ruby for sets with repeated elements

How do we get intersections and unions in Ruby for sets that repeat elements. # given the sets a = ["A", "B", "B", "C", "D", "D"] b = ["B", "C", "D", "D", "D", "E"] # A union function that adds repetitions union(a, b) => ["A", "B", "B", "C", "D",…
Amin Shah Gilani
  • 5,759
  • 3
  • 23
  • 59
1
2 3 4 5 6