[問題] 一行表列 ['cat','dog','rabbit'] 的字元

作者: fourdollars (四元)   2015-01-20 22:18:08
最近在看 http://interactivepython.org/runestone/static/pythonds/index.html
看到 Control Structures 最底下有個自我測驗
原本是做到像是下一行就可以了
> print([ch for word in ['cat','dog','rabbit'] for ch in word])
但是看到 "For an extra challence, see if you can figure out how to remove the
duplicates." 然後又看了底下的影片覺得用 list 包住 set 再包原本的 list 有點鳥
請問有沒有人有其它不同的一行解法嗎?
作者: ocean5566 (煙大屌熟男)   2015-01-20 22:42:00
"".join(['cat','dog','rabbit'])
作者: qoorocker (rockers)   2015-01-20 22:43:00
''.join(['cat', 'dog', 'rabbit'])
作者: alibuda174 (阿哩不達)   2015-01-21 00:01:00
list({c for w in ['cat','dog','rabbit']for c in w)
作者: changyuheng (張昱珩)   2015-01-21 11:31:00
l = ['c', 'a', 't', 'd', 'o', 'g', 'r', 'a', ...[c for i, c in enumerate(l) if c not in l[:i]]或 [c for c in set(l)],不過 set 不保證順序,若要順序一致可以參考 OrderedSet http://bit.ly/1C7ns1Ot = []; [c for w in l for c in w if c not in t \and t.extend(c)]上面這行是當 l = ['cat', 'dog', 'rabbit'] 時。

Links booklink

Contact Us: admin [ a t ] ucptt.com