wordlist = ['cat','dog','rabbit']
letterlist = [ ]
要列出所有字符的所有單詞,我們可以這樣做: letterlist = [word[i] for word in wordlist for i in range(len(word))]
['c', 'a', 't', 'd', 'o', 'g', 'r', 'a', 'b', 'b', '
我有一個看起來像alist = ['4', '1.6', 'na', '2e-6', '42']的python列表。如果我想從這個刪除引號,並使它看起來像[4, 1.6, na, 2e-6, 42],通常我使用下面的代碼: alist = [float(x) if type(x) is str else None for x in alist]
但是這一次,因爲我有串'na'在列表中的元素之一