python set型

和集合、積集合、部分集合などの計算の為、pythonのsetを

s = {1, 2, 2, 3, 1, 4}

print(s)
print(type(s))

[vagrant@localhost python]$ python app.py
{1, 2, 3, 4}

s = {i**2 for i in range(5)}

print(s)

s = {1, 2, 2, 3, 1, 4}
print(s)
print(len(s))

[vagrant@localhost python]$ python app.py
{0, 1, 4, 9, 16}
{1, 2, 3, 4}
4

和集合

s1 = {0, 1, 2}
s2 = {1, 2, 3}
s3 = {2, 3, 4}

s_union = s1 | s2
print(s_union)

s_union = s1.union(s2)
print(s_union)

セットで計算

s1 = {0, 1, 2}
s2 = {1, 2, 3}
s3 = {2, 3, 4}

s_union = s1.union(s2,s3)
print(s_union)