如何计算字典中各种字符串的实例数
How to count the number of instances of various strings in a dictionary
我有一个很大的 ReviewID 字典(超过 10,000 个条目)。字典有两个键,第一个是 ReviewID #,第二个是评论的语言。
我的任务是计算每种语言的评论总数,然后将其显示在条形图中。
import pandas as pd
import csv
import matplotlib.pyplot as plt
import sys
RevDict = {}
with open('ReviewID.txt','r') as f:
for line in f:
a,b = line.split(":")
RevDict[a] = str(b)
这会生成如下所示的字典:
我的想法是将字典转换为 Dataframe,其中 Review ID 是一列,语言是第二列。然后我可以使用计数器遍历行并最终得到每种语言的最终计数。这可以很容易地转换成条形图。
不幸的是,我不知道该怎么做。
我还怀疑更 pythonic 的方法是简单地计算字典本身中每个字符串的实例数,而不是通过制作数据框的步骤。我试过这个:
from collections import Counter
Counter(k['b'] for k in data if k.get('b'))
它抛出以下错误:
AttributeError: 'str' 对象没有属性 'get'
在您的 for k in data
循环中,每个 k
都是一个字符串键(评论 ID)。字符串没有 .get()
方法,原始变量 b
与此循环没有任何关系。
如果你想计算值,只需将字典的值直接传递给 Counter
:
Counter(data.values())
您可能想先删除换行符:
for line in f:
review_id, lang = line.split(":")
RevDict[review_id] = lang.strip()
使用collections.Counter
import collections as coll
data = {
'A': 'English',
'B': 'German',
'C': 'English'
}
print(coll.Counter(data.values()))
--output:--
Counter({'English': 2, 'German': 1})
使用pandas
:
import pandas as pd
data = {
'A': 'fr\n',
'B': 'de\n',
'C': 'fr\n',
'D': 'de\n',
'E': 'fr\n',
'F': 'en\n'
}
df = pd.DataFrame(
{
'id': list(data.keys()),
'lang': [val.rstrip() for val in data.values()],
}
)
print(df)
输出:
id lang
0 B de
1 A fr
2 F en
3 D de
4 E fr
5 C fr
grouped = df.groupby('lang')
print(grouped.size())
输出:
lang
de 2
en 1
fr 3
回复评论
Plotting
:
import collections as coll
import matplotlib.pyplot as plt
import numpy as np
from operator import itemgetter
data = {
'A': 'fr\n',
'B': 'de\n',
'C': 'fr\n',
'D': 'de\n',
'E': 'fr\n',
'F': 'en\n'
}
counter = coll.Counter(
[val.rstrip() for val in data.values()]
)
langs, lang_counts = zip(
*sorted(counter.items(), key=itemgetter(1))
)
total_langs = sum(lang_counts)
bar_heights = np.array(lang_counts, dtype=float) / total_langs
x_coord_left_side_of_bars = np.arange(len(langs))
bar_width = 0.8
plt.bar(
x_coord_left_side_of_bars,
bar_heights,
bar_width,
)
plt.xticks(
x_coord_left_side_of_bars + (bar_width * 0.5), #position of tick marks
langs #labels for tick marks
)
plt.xlabel('review language')
plt.ylabel('% of all reviews')
x = plt.plot()
#plt.show() #Can use show() instead of savefig() until everything works correctly
plt.savefig('lang_plot.png')
剧情:
我有一个很大的 ReviewID 字典(超过 10,000 个条目)。字典有两个键,第一个是 ReviewID #,第二个是评论的语言。
我的任务是计算每种语言的评论总数,然后将其显示在条形图中。
import pandas as pd
import csv
import matplotlib.pyplot as plt
import sys
RevDict = {}
with open('ReviewID.txt','r') as f:
for line in f:
a,b = line.split(":")
RevDict[a] = str(b)
这会生成如下所示的字典:
我的想法是将字典转换为 Dataframe,其中 Review ID 是一列,语言是第二列。然后我可以使用计数器遍历行并最终得到每种语言的最终计数。这可以很容易地转换成条形图。
不幸的是,我不知道该怎么做。
我还怀疑更 pythonic 的方法是简单地计算字典本身中每个字符串的实例数,而不是通过制作数据框的步骤。我试过这个:
from collections import Counter
Counter(k['b'] for k in data if k.get('b'))
它抛出以下错误:
AttributeError: 'str' 对象没有属性 'get'
在您的 for k in data
循环中,每个 k
都是一个字符串键(评论 ID)。字符串没有 .get()
方法,原始变量 b
与此循环没有任何关系。
如果你想计算值,只需将字典的值直接传递给 Counter
:
Counter(data.values())
您可能想先删除换行符:
for line in f:
review_id, lang = line.split(":")
RevDict[review_id] = lang.strip()
使用collections.Counter
import collections as coll
data = {
'A': 'English',
'B': 'German',
'C': 'English'
}
print(coll.Counter(data.values()))
--output:--
Counter({'English': 2, 'German': 1})
使用pandas
:
import pandas as pd
data = {
'A': 'fr\n',
'B': 'de\n',
'C': 'fr\n',
'D': 'de\n',
'E': 'fr\n',
'F': 'en\n'
}
df = pd.DataFrame(
{
'id': list(data.keys()),
'lang': [val.rstrip() for val in data.values()],
}
)
print(df)
输出:
id lang
0 B de
1 A fr
2 F en
3 D de
4 E fr
5 C fr
grouped = df.groupby('lang')
print(grouped.size())
输出:
lang
de 2
en 1
fr 3
回复评论
Plotting
:
import collections as coll
import matplotlib.pyplot as plt
import numpy as np
from operator import itemgetter
data = {
'A': 'fr\n',
'B': 'de\n',
'C': 'fr\n',
'D': 'de\n',
'E': 'fr\n',
'F': 'en\n'
}
counter = coll.Counter(
[val.rstrip() for val in data.values()]
)
langs, lang_counts = zip(
*sorted(counter.items(), key=itemgetter(1))
)
total_langs = sum(lang_counts)
bar_heights = np.array(lang_counts, dtype=float) / total_langs
x_coord_left_side_of_bars = np.arange(len(langs))
bar_width = 0.8
plt.bar(
x_coord_left_side_of_bars,
bar_heights,
bar_width,
)
plt.xticks(
x_coord_left_side_of_bars + (bar_width * 0.5), #position of tick marks
langs #labels for tick marks
)
plt.xlabel('review language')
plt.ylabel('% of all reviews')
x = plt.plot()
#plt.show() #Can use show() instead of savefig() until everything works correctly
plt.savefig('lang_plot.png')
剧情: