words = 'The quick brown fox jumps over the lazy dog'.split()
print words
这就是清单。我一直在尝试一些代码来获得3个字母但没有运气?这就是我到目前为止得到的三个字母:
for word in words:
wordsByLength[ len(word) ].append( word )
但我想我可能会采取错误的方式。
答案 0 :(得分:3)
使用理解来迭代你制作的列表,然后随时检查长度:
three_letters = [word for word in words if len(word) == 3]
答案 1 :(得分:-1)
len_three = []
for i in words:
if len(i) == 3:
len_three.append(i)
print len_three # ['The', 'fox', 'the', 'dog']