[问题] 一行表列 ['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