集合の基本操作と基本演算
38208 ワード
集合の基本操作と基本演算
しゅうごう
集合は無秩序で、重複せず変更できないデータ集合であり、その中の要素はハッシュ可能(可変タイプ)であるが、集合自体はハッシュ不可(したがって集合は辞書のキーを作れない)である.以下は、集合の最も重要な2点です.
コレクションの作成
空のコレクションを作成
ここで注意すべきことは、カッコ「{}」を直接使ってはいけません.
>>> st=set()
>>> st
set()
>>> st1={}
>>> type(st),type(st1)
(<class 'set'>, <class 'dict'>)
>>>
マルチエレメントのコレクションの作成
>>> st={"a","b","c","d","e"}
>>> st,type(st)
({'a', 'd', 'b', 'c', 'e'}, <class 'set'>)
>>>
集合の強転
>>> li=[1,"a","n","university"]
>>> st_li=set(li)
>>> st_li,type(st_li)
({1, 'n', 'university', 'a'}, <class 'set'>)
>>>
>>> str="universitysity"
>>> st_str=set(str)
>>> st_str,type(st_str)
({'v', 'r', 'i', 'n', 'y', 'e', 's', 'u', 't'}, <class 'set'>)
>>>
>>> dic={"a":1,"b":2,"c":3,"d":4}
>>> st_dic=set(dic)
>>> st_dic,type(dic)
({'d', 'b', 'c', 'a'}, <class 'dict'>)
>>>
>>> tp=(1,2,3,4)
>>> st_tp=set(tp)
>>> st_tp,type(st_tp)
({1, 2, 3, 4}, <class 'set'>)
>>>
集合の基本操作
調べる
無秩序、検索不可
>>> st={1,2,"Hunan","Yingxiao","College"}
>>> st[2]
Traceback (most recent call last):
File "" , line 1, in <module>
TypeError: 'set' object does not support indexing
>>>
増加
>>> st={1,2,"Hunan","Yingxiao","College"}
>>> st.add("Computer Science")
>>> st
{1, 2, 'Computer Science', 'Yingxiao', 'Hunan', 'College'}
>>> st.update(" "," ")
>>> st
{1, 2, ' ', 'Computer Science', 'Yingxiao', 'Hunan', ' ', 'College', ' ', ' '}
>>>
削除
>>> st={1,2,"Hunan","Yingxiao","College"}
>>> st.pop()#
1
>>> st
{2, 'Yingxiao', 'Hunan', 'College'}
>>> st={1,2,"Hunan","Yingxiao","College"}
>>> st.discard(2)#
>>> st
{1, 'Yingxiao', 'Hunan', 'College'}
>>> st.discard("Hunan")#
>>> st
{1, 'Yingxiao', 'College'}
>>> st.discard("YiYang")#
>>> st
{1, 'Yingxiao', 'College'}
>>> st={1,2,"Hunan","Yingxiao","College"}
>>> st.remove(2)
>>> st
{1, 'Yingxiao', 'Hunan', 'College'}
>>> st.remove("HUNAN")#
Traceback (most recent call last):
File "" , line 1, in <module>
KeyError: 'HUNAN'
>>>
>>> st={1,2,3}
>>> st.clear()
>>> st
set()
>>> st={1,2,3}
>>> del st
>>> st
Traceback (most recent call last):
File "" , line 1, in <module>
NameError: name 'st' is not defined
>>>
変更、変更不可
>>> st={1,2,"Hunan","Yingxiao","College"}
>>> st[1]="Hunan"
Traceback (most recent call last):
File "" , line 1, in <module>
TypeError: 'set' object does not support item assignment
>>>
遍歴する
>>> st={1,2,"Hunan","Yingxiao","College"}
>>> for i in st:
... print(i,end=" ")
...
1 2 Yingxiao Hunan College >>>
>>>
>>> st={1,2,"Hunan","Yingxiao","College"}
>>> for index,value in enumerate(st):
... print(index,value)
...
0 1
1 2
2 Yingxiao
3 Hunan
4 College
>>>
比較
集合の基本演算
サブセット
>>> a=set("abcd")
>>> b=set("cdef")
>>> c=set("ab")
>>> a,b,c
({'d', 'b', 'c', 'a'}, {'d', 'c', 'f', 'e'}, {'b', 'a'})
>>> c.issubset(a)
True
>>> c<a
True
>>> c<b
False
>>>
交差
>>> a=set("abcd")
>>> b=set("cdef")
>>> a.intersection(b)
{'d', 'c'}
>>> a&b
{'d', 'c'}
>>>
へいれつ
>>> a=set("abcd")
>>> b=set("cdef")
>>> a.union(b)
{'a', 'd', 'b', 'c', 'e', 'f'}
>>> a|b
{'a', 'd', 'b', 'c', 'e', 'f'}
>>>
調査セット
>>> a=set("abcd")
>>> b=set("cdef")
>>> a.difference(b)
{'b', 'a'}
>>> a-b
{'b', 'a'}
>>> b-a
{'f', 'e'}
>>>
>>> a=set("abcd")
>>> b=set("cdef")
>>> c=set("ab")
>>> a,b,c
({'d', 'b', 'c', 'a'}, {'d', 'c', 'f', 'e'}, {'b', 'a'})
>>> c.issubset(a)
True
>>> c<a
True
>>> c<b
False
>>>
>>> a=set("abcd")
>>> b=set("cdef")
>>> a.intersection(b)
{'d', 'c'}
>>> a&b
{'d', 'c'}
>>>
>>> a=set("abcd")
>>> b=set("cdef")
>>> a.union(b)
{'a', 'd', 'b', 'c', 'e', 'f'}
>>> a|b
{'a', 'd', 'b', 'c', 'e', 'f'}
>>>
>>> a=set("abcd")
>>> b=set("cdef")
>>> a.difference(b)
{'b', 'a'}
>>> a-b
{'b', 'a'}
>>> b-a
{'f', 'e'}
>>>