计数器python中文本文件中的字符



我在python中有一个文本文件。这个文本文件是这样的:

Aragon is an autonomous community in northeastern Spain. The capital of Aragon is Zaragoza, which is also the most populous city in the autonomous community. Covering an area of ​​47720 km2, the region's terrain ranges from permanent glaciers through verdant valleys, rich pastures and orchards to the arid steppe plains of the central lowlands. Aragon is home to many rivers, most notably the Ebro, Spain's largest river, which flows west to east throughout the region through the province of Zaragoza. It is also home to the highest mountains in the Pyrenees.

现在我想从集合中创建一个dict来计算每个字符出现的次数。

我给出了以下代码用于阅读文本文件

with open("data/aragon.txt",'r') as data_file:
for line in data_file:
data = line.lower().strip().split()
print(data)

现在我想知道是否有一种更容易的方法来计算每个字符的出现,或者这是的唯一方法

from collections import Counter
Counter(['A','r','a','g','o','n','i','s','a','n','a','u','t','o','n','o','m','o','u','s','c','o','m','m','u','n','i','t','y','i','n','n','o','r','t','h','e','a','s','t','e','r','n','S','p','a','i','n','.'])

我这么做只是为了第一句话。但我想知道是否有更简单的方法。

您已经接近了,计数器对象有一个类似字典的接口,因此,您可以用字符串调用它上的update来更新其内部内容。

from collections import Counter
counter = Counter()
with open("data/aragon.txt",'r') as data_file:
for line in data_file:
data = line.lower().strip()  # no split is needed
counter.update(data)
print(data)
print(counter)

最新更新