简体   繁体   English

如何将 txt.file 中的字符串与我的字典值(值是整数)进行比较并计算总和

[英]How do I compare the strings in a txt.file to my dictionary values (the values are integers) and calculate the sum

The title of my question might be a bit confusing - maybe that's because I am a little confused myself.我的问题的标题可能有点混乱 - 也许那是因为我自己有点困惑。

I have a task to create a small billing system for a restaurant (It is not a real restaurant).我的任务是为餐厅创建一个小型计费系统(它不是真正的餐厅)。 I have 3 text files.我有 3 个文本文件。 The first one being the menu.txt.第一个是 menu.txt。 In the file, each line consists of the name of a dish and its price, seperated by a comma.在文件中,每一行都包含一道菜的名称和价格,用逗号分隔。 Then I have 2 order files order1.txt and order2.txt Each line contains an item from the menu that has been ordered.然后我有 2 个订单文件 order1.txt 和 order2.txt 每行都包含已订购的菜单中的一项。

My own suggestion to this task was to put the menu file into a list and then make it a dictionary.我自己对这项任务的建议是将菜单文件放入列表中,然后使其成为字典。

This is my solution:这是我的解决方案:

def compute_total(mfile, ofile):
    dictionary = {}
    newlist = []
 
    with open('menu.txt') as f:
        for line in f:
            line = line.replace(',', '')
            newlist.append(line.split())
               
        for strings in newlist:
           dictionary[strings[0]] = strings[1]

I feel like I am on the right way with this but I don't really know how make code from here.我觉得我做对了,但我真的不知道如何从这里制作代码。 Because I know that I want to somehow see if for example: order1 is in the dictionary (menu) and then calculate the values (prices) of the dishes.因为我知道我想以某种方式查看例如: order1 是否在字典(菜单)中,然后计算菜肴的价值(价格)。

I was thinking maybe something like this below, but it does not work and I am stuck.我在想下面可能是这样的,但它不起作用,我被卡住了。

    for k, v in dictionary.items():
        if int(k) in dictionary.items():
            Dvalues.append(v)

I hope you can give me some advice to get going.我希望你能给我一些建议,让我继续前进。 I am a novice so I really hope you would take some time to help me with small problem (for you) like this.我是一个新手,所以我真的希望你能花一些时间来帮助我解决这样的小问题(对你来说)。

Best regards, SEE最好的问候,见

Python's csv package is great for dealing with comma separated files. Python 的csv package 非常适合处理逗号分隔的文件。 Instead of having to parse each line manually, the csv module will be able to convert each line to a list of values that were previously separated by commas. csv 模块不必手动解析每一行,而是能够将每一行转换为以前用逗号分隔的值列表。 Then, you can just use parallel assignment to get the items and prices for each line, and store them in the dictionary:然后,您可以使用并行分配来获取每行的商品和价格,并将它们存储在字典中:

with open('menu.txt') as f:
    for line in f:
        item, price = csv.reader(line, quotechar="'", delimiter=",")
        dictionary[item] = price

Now that you've stored all the menu items in the dictionary, all you have to do is read each item line from the other text files, pass that item to your dictionary, get back the price, and add it to the sum:现在您已经在字典中存储了所有菜单项,您所要做的就是从其他文本文件中读取每个项目行,将该项目传递给您的字典,取回价格,并将其添加到总和中:

order1_total = 0
with open(mfile) as f:
    for line in f:
        item = line      
        order1_total += dictionary[item]
           

Don't forget to add the line import csv to the top of your program.不要忘记将行import csv添加到程序的顶部。 Hope it helps!希望能帮助到你! Good luck!祝你好运!

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

相关问题 我有一本字典 {string and int}。 如何比较整数值而不是同一字典中的键(python) - I have a dictionary {string and int}. How do I compare integers values and not the keys within the same dictionary(python) 如何将列表作为值的字典转换为整数作为值的字典? - How do I turn a dictionary with lists as values into a dictionary with integers as values? 如何比较字典中的值? - How do I compare values in a dictionary? 如何在不使用 sum() 的情况下从每个键中有多个值的字典中的平均值计算平均值? - How do I calculate an average from averages in a dictionary with multiple values in each key without using sum()? 如何将字典列表值转换为整数? - How do I convert dictionary list values to integers? 如何缩小字典中多个值的整数? - How do I scale down integers from multiple values in a dictionary? 使用 for 循环计算此字典中值的总和,不使用 sum() function - Using a for loop Calculate the sum of the values in this dictionary and do not use sum() function 我用字典为字符串列表赋值。 我如何总结这些值? - I assigned values to a list of strings with a dictionary. How can I sum these values? 如何将字典键的随机结果与2个值进行比较? - How do I compare random result against dictionary keys with 2 values? 如何计算字典中值的比率? - How do I calculate a ratio for values within a dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM