小编mat*_*ode的帖子

如何找到特定单词的哪一行.[蟒蛇]

我正在尝试创建一个程序,它计算行数,计算一个特定单词,然后告诉你它在哪一行.现在我做了前两个,但我不知道如何找到这个词的哪一行.有什么建议 ?这是我目前的代码:

 name = input('Enter name of the text file: ')+'.txt'

file = open(name,'r')

myDict = {}
linenum = 0

for line in file:
    line = line.strip()
    line = line.lower()
    line = line.split()
    linenum += 1

print(linenum-1)

count = 0
word = input('enter the word you want us to count, and will tell you on which line')
#find occurence of a word
with open(name, 'r') as inF:
    for line in inF:
        if word in line:
            count += 1

#find on …
Run Code Online (Sandbox Code Playgroud)

python

5
推荐指数
1
解决办法
8836
查看次数

在伪代码中返回-1的含义是什么

我有以下伪代码,顺序搜索伪代码,我试图理解返回-1的意思.我们为什么要返回-1,有人可以解释一下.

A[n] <-- K
i <-- 0
while A[i] != K do
     i = i + 1
if(i<n)
     return i;
else
     return -1;       //What is this mean ?
Run Code Online (Sandbox Code Playgroud)

c++ pseudocode

0
推荐指数
1
解决办法
261
查看次数

标签 统计

c++ ×1

pseudocode ×1

python ×1