如何计算文本文件中重复单词的数量?

时间:2018-08-22 18:13:40

标签: python python-3.x

自动售货机程序中的代码示例

item = int(input("What item do you want: [1-10]"))
item_chosen = open("items.txt","a")
item_chosen.write(str(item))
item_chosen_data = items_chosen.read()    
item_chosen.close()

在这里,我如何能够检查文本文件中存储的每个商品编号有多少(由于自动售货机在商品“不可用”之前应该只有有限的库存)?

2 个答案:

答案 0 :(得分:1)

item = int(input("What item do you want: [1-10]"))

# use the with open structure to work with files without having to explicitly close them
with open("items.txt","a+") as item_chosen:
    item_chosen.write(str(item))
    item_chosen_data = item_chosen.read()

# Use the string.split() method to get the item_chosen_data as a list instead of a string.
item_chosen_data = item_chosen_data.split()

# Create a set from the list of items, the removes duplicate values.
unique_items = set(item_chosen_data)

# Initialize a dictionary to map each item to it's count.
item_to_count = {}

# for each unique item
for item in unique_items:
    # map the item to the number of times it appears in the list (ie. list.count() method)
    item_to_count[item] = item_chosen_data.count(item)

我希望我的评论可以自我解释,但是如果需要更多信息,请告诉我。本质上,您希望将从文本文件中读取的信息分成一个列表,该列表使您可以使用list.count()方法。

此外,就像其他人提到的那样,在此任务中完全避免写入文件可能会有所帮助。一种替代方法是使用自动售货机中的每个商品编号定义字典,例如:

# build a dictionary mapping each item to it's stock
item_to_stock = {'coke': 3, 'pepsi': 2, 'apple_juice': 5, 'doritos': 3}

# put a coke into the vending machine:
item_to_stock['coke'] = item_to_stock.get('coke', 0) + 1

# take a pepsi out of the vending machine, first check if the item is in stock
if item_to_stock.get('pepsi') > 0:
    item_to_stock['pepsi'] = item_to_stock['pepsi'] - 1

答案 1 :(得分:0)

尽管它看起来像XY problem,但您可以为此使用计数器:

import collections

def count_items():
    with open('items.txt') as f:
        return collections.Counter(line.strip() for line in f)

计数器是dict的子类,因此您可以轻松检查任何项目的计数:

purchased_item_count = count_items()
print(purchased_item_count["9"])

应打印项目“ 9”的计数。

但是,理想情况下,您将使用某种数据库来完成此任务。重复打开文件,对其进行写入,将其关闭,再次打开,计算其内容并再次将其关闭是非常低效率的。