简体   繁体   English

Python:使用dict中的值对每一行求和

[英]Python: Sum each lines with their values from dict

dict = {'A': 71.07884,
    'B': 110,
    'C': 103.14484,
    'D': 115.08864,
    'E': 129.11552,
    'F': 147.1766,
    'G': 57.05196,
    'H': 137.1412
    }


def search_replace(search, replacement, searchstring):
    p = re.compile(search)
    searchstring = p.sub(replacement, searchstring)
    return (searchstring)


def main():
    with open(sys.argv[1]) as filetoread:
    lines = filetoread.readlines()
    file = ""

    for i in range(len(lines)):
        file += lines[i]

    file = search_replace('(?<=[BC])', ' ', file)

    letterlist = re.split('\s+', file)

    for j in range(len(letterlist)):
        print(letterlist[j])

if __name__ == '__main__':
    import sys
    import re
    main()

My program open a file and split the text of letters after B or C. 我的程序打开一个文件并在B或C之后拆分字母文本。

The file looks like: 该文件看起来像:

ABHHFBFEACEGDGDACBGHFEDDCAFEBHGFEBCFHHHGBAHGBCAFEEAABCHHGFEEEAEAGHHCF

Now I want to sum each lines with their values from dict. 现在我想用dict中的每个值与它们的值相加。

For example: 例如:

AB = 181.07884
HHFB = 531.4590000000001

And so on. 等等。

I dont know how to start. 我不知道如何开始。 Thanks a lot for all your answers. 非常感谢您的所有答案。

Try to simplify things... 试着简化一些事情......

Given you already have a string s and a dictionary d : 鉴于你已经有一个字符串s和一个字典d

ctr = 0
temp = ''
for letter in s:
    ctr += d[letter]
    temp += letter
    if letter in 'BC':
        print(temp, ctr)
        ctr = 0
        temp = ''

In the case you supplied where: 在您提供的情况下:

s = "ABHHFBFEACEGDGDACBGHFEDDCAFEBHGFEBCFHHHGBAHGBCAFEEAABCHHGFEEEAEAGHHCF"
d = {'A': 71.07884,
'B': 110,
'C': 103.14484,
'D': 115.08864,
'E': 129.11552,
'F': 147.1766,
'G': 57.05196,
'H': 137.1412
}

You get the results (printed to terminal): 你得到结果(打印到终端):

>>> ('AB', 181.07884)
('HHFB', 531.4590000000001)
('FEAC', 450.5158)
('EGDGDAC', 647.6204)
('B', 110)
('GHFEDDC', 803.8074)
('AFEB', 457.37096)
('HGFEB', 580.4852800000001)
('C', 103.14484)
('FHHHGB', 725.6521600000001)
('AHGB', 375.272)
('C', 103.14484)
('AFEEAAB', 728.64416)
('C', 103.14484)
('HHGFEEEAEAGHHC', 1571.6099199999999)

You already did most of the work! 你已经完成了大部分的工作! All you miss out is the sum for each substring. 你错过的就是每个子串的总和。

As substrings can occur more often, I'll do the summation only once, and store the values for each substring encountered in a dict (and your above dict for the relation of letter to value I renamed to mydict in order to avoid keyword confustion): 由于子字符串可以更频繁地出现,我只进行一次求和,并存储dict中遇到的每个子字符串的值(以及上面的字母表中为了避免关键字混淆而重命名为mydict的字母与值的关系) :

snippets = {}
for snippet in letterlist:
    if snippet not in snippets:
        value = 0
        for s in snippet:
            value += mydict.get(s)
        snippets[snippet] = value
print(snippets)

That gives me an output of 这给了我一个输出

{
'AB': 181.07884, 
'HHFB': 531.4590000000001, 
'FEAC': 450.5158, 
'EGDGDAC': 647.6204, 
'B': 110, 
'GHFEDDC': 803.8074, 
'AFEB': 457.37096, 
'HGFEB': 580.4852800000001, 
'C': 103.14484, 
'FHHHGB': 725.6521600000001, 
'AHGB': 375.272, 
'AFEEAAB': 728.64416, 
'HHGFEEEAEAGHHC': 1571.6099199999999, 
'F': 147.1766}

Open you file and then read each character, then find the character on the dictionary and add the value to your total. 打开你的文件,然后阅读每个字符,然后找到字典上的字符并将值添加到总数中。

sum_ = 0
letters = "letters_file"
opened = open(letters, "r")
for row in opened:
    for char in row:
        sum_ += int(your_dictionary[char])

print(sum_)

You can use re.split with itertools.zip_longest in a dict comprehension: 你可以在dict理解中使用re.splititertools.zip_longest

import re
from itertools import zip_longest
i = iter(re.split('([BC])', s))
{w: sum(d[c] for c in w)for p in zip_longest(i, i, fillvalue='') for w in (''.join(p),)}

This returns: 返回:

{'AB': 181.07884, 'HHFB': 531.4590000000001, 'FEAC': 450.5158, 'EGDGDAC': 647.6204, 'B': 110, 'GHFEDDC': 803.8074, 'AFEB': 457.37096, 'HGFEB': 580.4852800000001, 'C': 103.14484, 'FHHHGB': 725.6521600000001, 'AHGB': 375.272, 'AFEEAAB': 728.64416, 'HHGFEEEAEAGHHC': 1571.6099199999999, 'F': 147.1766}

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

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