Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.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 如何返回给定.csv文件的值之和?_Python_List_Python 2.7_Parsing_Csv - Fatal编程技术网

Python 如何返回给定.csv文件的值之和?

Python 如何返回给定.csv文件的值之和?,python,list,python-2.7,parsing,csv,Python,List,Python 2.7,Parsing,Csv,我有一个这样的.csv文件,它有文字和值: string1, 102, 90, 23 string2, 89, 45, 21 ... hi, 1, 3, 5 example, 2, 0, 2 someone, 1, 1, 1 hope, 0, 0, 0 stringN, 923, 23892, 9292 stringnN-1, 2903, 49058, 4859 还有一大堆这样的词: lis__ = [[Hi this is an example, this site is nice!.],.

我有一个这样的.csv文件,它有文字和值:

string1, 102, 90, 23
string2, 89, 45, 21
...
hi, 1, 3, 5
example, 2, 0, 2
someone, 1, 1, 1
hope, 0, 0, 0
stringN, 923, 23892, 9292
stringnN-1, 2903, 49058, 4859
还有一大堆这样的词:

lis__ = [[Hi this is an example, this site is nice!.],...,[I hope someone can help]]
如何返回出现在
lis\uu
中的每个单词的值之和。对于上述实例,输出如下:

对于第一个子列表:

    [Hi this is an example, this site is nice!.]

In:
    hi, 1, 3, 5
    example, 2, 0, 2
    someone, 1, 1, 1
    hope, 0, 0, 0

Then add value one with value one, two with two and three with three:

Out:
[(3,3,7)]
然后,对于第二个子列表,将值1与值1相加,将值2与值2相加,将值3与值3相加:

In:
    [I hope someone can help]
    hi, 1, 3, 5
    example, 2, 0, 2
    someone, 1, 1, 1
    hope, 0, 0, 0
out:
    [(1,1,1)]
最后:

[(3,3,7),...,(1,1,1)]
其中,
是无限多个字符串或元组。也许这个任务可以通过
csv
模块完成,你知道怎么做吗?。提前谢谢各位

那么:

import csv
import re

class Score(object):
    def __init__(self, *args):
        self.lst = args

    def __repr__(self):
        return str(tuple(self.lst))

    def __iadd__(self, other):
        new = [self.lst[i] + other.lst[i] for i in range(3)]
        return Score(*new)


lis__ = [
    'Hi this is an example, this site is nice!.',
    'I hope someone can help',
]

# Build word_scores dictionary, keyed by word
word_scores = {}
with open('yourcsv.csv') as f:
    reader = csv.reader(f)
    for line in reader:
        word_scores[line[0].lower()] = Score(*map(int, line[1:]))


# Loop over lis__, computing the total score for each element (elem_score),
#    append it to line_scores
line_scores = []
for elem in lis__:
    elem_score = Score(0,0,0)
    for word in re.split(r'[^\w]+', elem):
        try:
            score = word_scores[word.lower()]
            print("  Found: %s %s" % (word.lower(), score))
            elem_score += score
        except KeyError:
            pass
    print("%s : %s" % (elem_score, elem))
    line_scores.append(elem_score)

print
print "Line Scores:"
print line_scores
输出:

Found: hi (1, 3, 5) Found: example (2, 0, 2) (3, 3, 7) : Hi this is an example, this site is nice!. Found: hope (0, 0, 0) Found: someone (1, 1, 1) (1, 1, 1) : I hope someone can help Line Scores: [(3, 3, 7), (1, 1, 1)] 发现:hi(1,3,5) 发现:示例(2,0,2) (3,3,7):嗨,这是一个例子,这个网站很好!。 发现:希望(0,0,0) 找到:某人(1,1,1) 我希望有人能帮忙 分数线: [(3, 3, 7), (1, 1, 1)]
尝试将
Score(*map(int,line[1:])
更改为
Score(*map(float,line[1:])
Score(*map(lambda x:int(float(x)),line[1:])
当然抱歉,我刚刚意识到了浮点。非常感谢你的帮助。我正在试着看看总数是多少。
lis_u_;
应该是这样的:
lis__;=[[['嗨,这是一个例子,这个网站很好!]],['我希望有人能帮助]]
而不是一个列表。是一个列表列表。是否知道如何继续?如果所有子列表都是单个元素,请将re.split中的word(r'[^\w]+',elem)的
更改为re.split中的word(r'[^\w]+',elem[0]):