Re: [問題] 集合 set 問題

看板Python作者 (迅雷不及掩耳盜鈴)時間6年前 (2019/07/16 10:56), 6年前編輯推噓1(100)
留言1則, 1人參與, 5年前最新討論串2/2 (看更多)
※ 引述《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

10/08 20:36, 5年前 , 1F
推詳盡
10/08 20:36, 1F
文章代碼(AID): #1TBJpJW4 (Python)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
文章代碼(AID): #1TBJpJW4 (Python)