简体   繁体   English

python中不支持的操作数类型错误

[英]Unsupported operand type error in python

def get_word_count(wordlist, final):
    regex = []
    count = [[] for x in xrange(len(wordlist))]
    frequency = []
    regex = makeregex(wordlist)
    for i in range(len(final)-1):
        size = os.stat(final[i]).st_size
        fil = open(final[i])
        if(fil):
            print final[i] + " read!"
            data = mmap.mmap(fil.fileno(), size, access=mmap.ACCESS_READ)
            for j in range (len(wordlist)):
                count[j].append(re.findall(regex[j], data))
        fil.close()
    for k in range(len(wordlist)):
        frequency.append(sum(count[k]))
    print frequency

count is a list of lists and every list has some numbers stored into it. count是一个列表列表,每个列表中都存储有一些数字。 I wish to store the sum of every list as an element to a new list frequency 我希望将每个列表的总和存储为新列表frequency的元素

When I run the code I get an error : 运行代码时出现错误:

Traceback (most recent call last):
File "C:\Users\Animesh\Desktop\_zipf.py", line 52, in <module>
get_word_count(wordlist, final)
File "C:\Users\Animesh\Desktop\_zipf.py", line 32, in get_word_count
frequency.append(sum(count[k]))
TypeError: unsupported operand type(s) for +: 'int' and 'list'

What should I change in my code ? 我应该更改我的代码吗? Please help 请帮忙

count[j].append(re.findall(regex[j], data))

You're adding list of found words by the regex to the array count[j] , so each count element is a list of list of strings, thus the error when calling sum(count[k]) . 您将通过正则表达式将找到的单词列表添加到数组count[j] ,因此每个count元素都是字符串列表的列表,因此调用sum(count[k])时出错。

I think you want to append to count[k] the number of found words: 我认为您想追加对找到的单词数进行count[k]

count[j].append(len(re.findall(regex[j], data)))

If you want to make it simpler, you could get rid of the count = [[] for x in xrange(len(wordlist))] and just have count = [] and later in the for loop you make it increment a temporary variable and append that to count after the for loop. 如果您想简化它,可以摆脱count = [[] for x in xrange(len(wordlist))]而只需让count = [] ,然后在for循环中,使它递增一个临时变量并将其附加在for循环之后进行计数。

size = 0
for j in range (len(wordlist)):
    size += len(re.findall(regex[j], data)) #thanks to CharlesB for this bit
count.append(size) #you could also cut out the middle man and just append frequency 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM