下面是一个列表理解。列表推导本质上是编写 for 循环的强大速记。一个基本的列表推导式采用[expr for variable in iterable]
. 它遍历 中的每个值iterable
,将其分配给variable
然后将结果存储expr
在列表中。所以
WordLenLi = [len(word) for word in st.split()]
print(WordLenLi)
生产
>>>
[5, 2, 3, 7, 6, 8, 6, 3, 2, 3, 4, 5, 6, 6, 3, 7, 8, 4, 5, 4, 6, 5]
作为一个 for 循环,它看起来像这样
WordLenLi = []
for word in st.split(): #for each word in a list of words
WordLenLi.append(len(word)) #insert the length of the word into WordLenLi
Alternativley,作为演示:
WordLenLi = [(word,len(word)) for word in st.split()]
print(WordLenLi)
>>>
[('April', 5), ('is', 2), ('the', 3), ('crueles', 7), ('month,', 6), ('breeding', 8), ('Lilacs', 6), ('out', 3), ('of', 2), ('the', 3), ('dead', 4), ('land,', 5), ('mixing', 6), ('Memory', 6), ('and', 3), ('desire,', 7), ('stirring', 8), ('Dull', 4), ('roots', 5), ('with', 4), ('spring', 6), ('rain.', 5)]
你也可以让它比第一个理解更短:
WordLenLi = map(len,st.split())
另外,根据乔恩克莱门特的建议,你想st.split()
用这样的东西代替:
re.findall(r'\b[\w\d%s]+\b' % string.punctuation,st)
这将要求您通过导入re和string模块import re,string
。