Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计算数组中项目的出现次数_Python_Arrays_Loops_Python 3.x - Fatal编程技术网

Python 计算数组中项目的出现次数

Python 计算数组中项目的出现次数,python,arrays,loops,python-3.x,Python,Arrays,Loops,Python 3.x,这个程序的目的是读入一个文件,把所有的字都变成单独的标记,然后把这些标记放到一个数组中。然后程序删除所有标点符号,并将所有字母更改为小写。然后,程序应该计算每个命令行参数在数组中出现的次数,并打印结果。我的程序能够成功地创建一个非定时的小写令牌数组。我现在的问题是如何循环数组并计算特定单词的出现次数,以及如何在主函数中调用这些函数。我的取消守时功能的工作方式与所写的一样 这是我的节目: import sys from scanner import * def main(): print

这个程序的目的是读入一个文件,把所有的字都变成单独的标记,然后把这些标记放到一个数组中。然后程序删除所有标点符号,并将所有字母更改为小写。然后,程序应该计算每个命令行参数在数组中出现的次数,并打印结果。我的程序能够成功地创建一个非定时的小写令牌数组。我现在的问题是如何循环数组并计算特定单词的出现次数,以及如何在主函数中调用这些函数。我的取消守时功能的工作方式与所写的一样

这是我的节目:

import sys
from scanner import *

def main():
    print("the name of the program is",sys.argv[0])
    for i in range(1,len(sys.argv),1):
        print("   argument",i,"is", sys.argv[i])
    tokens = readTokens("text.txt")
    cleanTokens = depunctuateTokens(tokens)
    words = [token.lower() for token in cleanTokens]
    count = find(words)
    print(words)
    print(count)
def readTokens(s):
    arr=[]
    s=Scanner("text.txt")
    token=s.readtoken()
    while (token != ""):
        arr.append(token)
        token=s.readtoken()
    s.close()
    return arr

def depunctuateTokens(arr):
    result=[]
    for i in range(0,len(arr),1):
        string=arr[i]
        cleaned=""
        punctuation="""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
        for i in range(0,len(string),1):
            if string[i] not in punctuation:
                cleaned += string[i]
        result.append(cleaned)
    return result

def find(tokens,words):
    return occurences(tokens,words)>0

def occurences(tokens,words):
    count = 0
    for i in range(0,len(words),1):
        if (words[i] == tokens):
            count += 1
        return count

main()
导入系统 从扫描仪导入* def main(): 打印(“程序名为”,sys.argv[0]) 对于范围内的i(1,len(sys.argv),1): 打印(“参数”,i,“is”,sys.argv[i]) tokens=readTokens(“text.txt”) cleanTokens=depuncutatetokens(令牌) words=[token.lower()表示cleanTokens中的令牌] 计数=查找(单词) 印刷品(字) 打印(计数) def读取令牌: arr=[] s=扫描仪(“text.txt”) token=s.readtoken() while(令牌!=“”): arr.append(令牌) token=s.readtoken() s、 关闭() 返回arr def解守时(arr): 结果=[] 对于范围内的i(0,len(arr),1): string=arr[i] 已清理=“” 标点符号=”!“#$%&'()*+,-./:@[\]^_`{|}~""" 对于范围内的i(0,len(字符串),1): 如果字符串[i]不是标点符号: 清洁+=字符串[i] 结果。追加(已清理) 返回结果 def查找(标记、文字): 返回事件(令牌、单词)>0 def事件(标记、文字): 计数=0 对于范围内的i(0,len(字),1): 如果(单词[i]==代币): 计数+=1 返回计数 main()
使用
列表。计数

>>> l = [1,2,3,4,5,6,7,44,4,4,4,4]
>>> print(l.count(4))
>>> 5

使用
list.count

>>> l = [1,2,3,4,5,6,7,44,4,4,4,4]
>>> print(l.count(4))
>>> 5

使用
list.count

>>> l = [1,2,3,4,5,6,7,44,4,4,4,4]
>>> print(l.count(4))
>>> 5

使用
list.count

>>> l = [1,2,3,4,5,6,7,44,4,4,4,4]
>>> print(l.count(4))
>>> 5

您现有的功能并不遥远:

def occurences(tokens,words):
    count = 0
    for i in range(0,len(words),1):
        if (words[i] == tokens):
            count += 1
        return count

第一个问题是,您在
for
循环中缩进了
返回计数。这意味着每次通过循环它都将
返回
,这意味着它将只处理第一个单词。因此,如果第一个单词匹配,它将返回1,否则返回0。只要取消输入
返回
,问题就解决了她把她带走了


第二个问题是,根据参数的名称判断,您希望
标记和
单词都是字符串列表
永远不会匹配整个标记列表。也许您想测试该单词是否匹配列表中的任何标记,而不是它是否匹配列表?在这种情况下,您可以编写:

if words[i] in tokens:

最后,虽然您的
find
函数似乎正确地调用了
occurrencess
(好吧,您拼写的
occurrencess
错误,但您一直这样做,所以这没问题),但实际上您并没有正确地调用
find
,因此您永远无法到达这里。您的调用如下:

count = find(words)
def find(tokens,words):
for word in words:
    count = occurences(word, words)
    print('{}: {}'.format(word, count))
…但你的定义是这样的:

count = find(words)
def find(tokens,words):
for word in words:
    count = occurences(word, words)
    print('{}: {}'.format(word, count))
你必须把一些东西传递给
tokens
参数。我不确定要传递什么,但你是设计和编写此代码的人;你编写此函数的目的是什么


我怀疑您真正寻找的是每个令牌的计数。在这种情况下,在您的设计中,
find
引用
实际上都应该使用单个
令牌
,而不是
令牌的列表
作为参数。在这种情况下,您不需要上面表达式中的
,您需要重命名参数。您不需要使用
find
,您只需要直接调用
occurrences
。您需要在循环中调用它,如下所示:

count = find(words)
def find(tokens,words):
for word in words:
    count = occurences(word, words)
    print('{}: {}'.format(word, count))

而且,正如您的其他两个函数正在复制已内置的函数一样(
str.translate
lower
),这个也是:
list.count
。如果你是为了学习而自己写的,那没关系,但如果这不是作业的一部分,就使用内置函数。

你现有的函数也不太远:

def occurences(tokens,words):
    count = 0
    for i in range(0,len(words),1):
        if (words[i] == tokens):
            count += 1
        return count

第一个问题是,您在
for
循环中缩进了
返回计数。这意味着每次通过循环它都将
返回
,这意味着它将只处理第一个单词。因此,如果第一个单词匹配,它将返回1,否则返回0。只要取消输入
返回
,问题就解决了她把她带走了


第二个问题是,根据参数的名称判断,您希望
标记和
单词都是字符串列表
永远不会匹配整个标记列表。也许您想测试该单词是否匹配列表中的任何标记,而不是它是否匹配列表?在这种情况下,您可以编写:

if words[i] in tokens:

最后,虽然您的
find
函数似乎正确地调用了
occurrencess
(好吧,您拼写的
occurrencess
错误,但您一直这样做,所以这没问题),但实际上您并没有正确地调用
find
,因此您永远无法到达这里。您的调用如下:

count = find(words)
def find(tokens,words):
for word in words:
    count = occurences(word, words)
    print('{}: {}'.format(word, count))
…但你的定义是这样的:

count = find(words)
def find(tokens,words):
for word in words:
    count = occurences(word, words)
    print('{}: {}'.format(word, count))
你必须把一些东西传递给
tokens
参数。我不确定要传递什么,但你是设计和编写此代码的人;你编写此函数的目的是什么


我怀疑您真正寻找的是每个令牌的计数。在这种情况下,在您的设计中,
find
引用
实际上都应该使用单个
令牌
,而不是
令牌的列表
作为参数。在这种情况下,您不需要上面表达式中的
,您需要重命名参数。并且您没有使用
查找
,只需调用
发生