Python 如何使用for循环实现段落字长列表

Python 如何使用for循环实现段落字长列表,python,python-2.7,Python,Python 2.7,我以前从未使用过stackoverflow,我通常会在数学和物理部分留宿。我是反应堆物理学家,不是程序员,这是我玩Python 2的第一周,所以请不要责怪我 我应该制作一个“wordLenLi”列表,其中包含使用for循环的小段落中的单词长度。短段落在批处理文件中 这就是我尝试过的。我还尝试过使用append()方法。这本小小的书没什么用 st = '''April is the crueles month, breeding Lilacs out of the dead land, mixin

我以前从未使用过stackoverflow,我通常会在数学和物理部分留宿。我是反应堆物理学家,不是程序员,这是我玩Python 2的第一周,所以请不要责怪我

我应该制作一个“wordLenLi”列表,其中包含使用for循环的小段落中的单词长度。短段落在批处理文件中

这就是我尝试过的。我还尝试过使用append()方法。这本小小的书没什么用

st = '''April is the crueles month, breeding
Lilacs out of the dead land, mixing
Memory and desire, stirring
Dull roots with spring rain.'''

x = st.upper()

wordLi = x.split(' ')

for n in wordLi:    
    z = len(n)
    WordLenli = z.split()
    print wordLenLi

下面是一个列表。列表理解本质上是编写循环的强大速记。基本列表理解的形式为
[expr for variable in iterable]
。它遍历
iterable
中的每个值,将其分配给
变量
,然后将
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())
同样,根据Jon Clement的建议,您可能希望用以下内容替换
st.split()

re.findall(r'\b[\w\d%s]+\b' % string.punctuation,st)

这将需要您通过导入re和string模块,导入re,string,因此我喜欢HennyH的答案,但只是为了让您不会觉得列表理解是唯一可能的答案,我们还有:

for word in paragraph.split() : 
    print(word.len())  
原始版本的问题是:

z = len(n)
WordLenli = z.split()
您试图“拆分”一个数字,就像它是一个字符串一样。一般的教训是:

  • 减少移动件的数量可以减少潜在的bug
  • 它有助于记住每个命名对象是什么类型的对象

我认为这些原理在物理上和在编程中一样有效,但当问题开始时,很容易忽略它们。

好吧,那不管用。等一下,我会一直到那里,直到我弄清楚如何发布我的codeCheck,它解释了标记格式。本质上,对于代码,只需将其缩进四个空格即可。(或者对于内联代码,用倒勾环绕-
`
)为什么我会被标记下来?答案是否需要大写?+1,这是最佳答案,但问题说明“使用for循环”,因此可能需要扩展(指向列表理解说明的链接,对于新手来说,他们可能会感到畏惧)为了教育的目的进入一个循环。除了使用NLTK解析段落外,最好将
str.split
替换为
re.findall('\w+',s)
或具有特定字符的类似字符,或使用
re.split
-的倒数,以不将标点作为字长计算(例如
month,
)@JonClements很好,我将根据您的观点更新答案注意,在3.x中,
map()
将生成一个生成器,而不是一个列表(虽然OP似乎使用2.x,所以对他们来说应该没问题)。感谢各位,这里的流量显然比physicsSE或mathSE上的流量大得多。谢谢你容忍我。谢谢你,亨利,现在。。。另一个问题。