我是python的新手,我对下面的循环使用感到困惑。任何人都可以帮助我理解下面forloop中的课程用法。
import sys
def checkline():
glb.linecount += 1
w = glb.l.split()
glb.wordcount += len(w)
class glb:
linecount = 0
wordcount = 0
l = []
f = open('Untitled9.ipynb','r')
for glb.l in f.readlines(): #what glb.l exactly does?
checkline()
print(glb.linecount, glb.wordcount)
答案 0 :(得分:1)
整个程序计算文件中的行和单词。特别,
glb.l
成为文件中的每一行,因此您可以迭代并计算每一行中的单词。
让我伪代码给你。
Open the file `Untitled9.ipynb` for reading. //f
For each line in the file: // checkline
Store the line.// youre adding the line to glb.l, which you will later iterate on to count the words in the file.
Add one to the line count.
For each space, add one to the word count. // counting the results of the split() on glb.l
Print the line and the word count.