<分区> 分区>
代码的目的是收集wordlist中的所有字符,我做了以下操作:
wordlist = ['cat','dog','rabbit']
[c for c in word for word in wordlist]
输出很奇怪:
['r',
'r',
'r',
'a',
'a',
'a',
'b',
'b',
'b',
'b',
'b',
'b',
'i',
'i',
'i',
't',
't',
't']
我知道我可以使用:
[ch for ch in "".join(wordlist)]
或
[word[i] for word in wordlist for i in range(len(word))]
不过我的第一个方案好像也对,谁能告诉我为什么第一个方案不行?
