How to join two sets in one line without using “|”

前端 未结 8 1041
闹比i
闹比i 2020-12-23 00:21

Assume that S and T are assigned sets. Without using the join operator |, how can I find the union of the two sets? This, for example,

8条回答
  •  失恋的感觉
    2020-12-23 00:46

    Assuming you also can't use s.union(t), which is equivalent to s | t, you could try

    >>> from itertools import chain
    >>> set(chain(s,t))
    set([1, 2, 3, 4, 5, 6])
    

    Or, if you want a comprehension,

    >>> {i for j in (s,t) for i in j}
    set([1, 2, 3, 4, 5, 6])
    

提交回复
热议问题