看板 Python 關於我們 聯絡資訊
※ 引述《h0304 (h0304)》之銘言: : 但我一直想不通 為何這2個結果會不一樣 : 1. s={'Hello'} : print(s) : => {'Hello'} s = { 'Hello' } 這邊是把 'Hello' 這個字串物件放入 set 裡作為一個元素 : 2. s=set ('Hello') : print(s) : => {'o','l','e','H'} s = set('Hello') 這邊是把 'Hello' 這個字串物件拆分之後的元素放入 set 裡 : 這兩種都是宣告s為集合 , 2種 print (type(s)) 都是 <class 'set'> : 為何第1種不會拆解文字,但第2種會拆解文字 ? https://docs.python.org/3.7/library/stdtypes.html#set The constructors for both classes work the same: class set([iterable]) class frozenset([iterable]) Return a new set or frozenset object whose elements are taken from iterable. The elements of a set must be hashable. To represent sets of sets, the inner sets must be frozenset objects. If iterable is not specified, a new empty set is returned. 所以你 set() 裡面是不能放非 iterable 的物件的 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.112.73.134 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1563245779.A.804.html ※ 編輯: Hsins (140.112.73.134 臺灣), 07/16/2019 10:57:07
purplvampire: 推詳盡 10/08 20:36