python 集合類型__python

來源:互聯網
上載者:User
There are two types of set:variable set and immutable set:variable set can not be hashed.Note:frozenset can not be changed!

1.create and update

> myset=set("abadsfljasd")> mysetset(['a', 'b', 'd', 'f', 'j', 'l', 's'])

Note:we can not create a set by operator(can not treated as list ant tuple or diretoty).we can only use set() anf frozenzet().
1.1visit the value in a set:

'a' in myset
1.2update a set

myset.add('z')myset.update("python")myset.remove("abcd")s-=set("python")

1.3delete a set del myset

2.the oprator of set 2.1 standard operator in,no in,==,!= , > , >=, <, <=

2.2 'set' type operator

combine: "|"> twoset=se("xyz")> oneset | twosetset(['a', 'c', 'b', 'd', 'y', 'x', 'z'])
&, -, ^
>oneset ^ threeset>set(['c', 'd', '1', '3', '2'])
mixed-type operate:set and frozen set> myset=set("abcd")> myfrozenset=frozenset("1234")> myset | myfrozensetset(['a', 'c', 'b', 'd', '1', '3', '2', '4'])> myfrozenset | mysetfrozenset(['a', 'c', 'b', 'd', '1', '3', '2', '4'])
Note:the result type follow the former.

2.3set-type operator---only used for set not for frozenset Update:|=, &=, -=, ^=

3.build_in function len(), set(), frozenset()

3.1 set-type build_in function Note:build_in function means standart build_in function that could used for set . set-type build_in function means this function is part of set-type.

function name           return value

s.add('a')              update s
s.clear()               clear s
s.difference(t)         a new set(element in s but not in t)
s.union(t)              a new set
s.intersection(t)       a new set
s.issubset(t)           True or False

3.2compare: operator and build_in function: For the most, build_in function and operarot is equal. However, the operand for the operator must be set-type, while the object for the build_in function could be iterative type.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.