我是一个非常新的Python,我试图分析数据集中的数据。Python:从数据集中收集数据
比方说,我有一个特定的食物品尝的数据集。例如:
{'review/appearance': 2.5, 'food/style': 'Cook', 'review/taste': 1.5, 'food/type': 'Vegetable' .... }
{'review/appearance': 5.0, 'food/style': 'Instant', 'review/taste': 4.5, 'food/type': 'Noodle' ....}
,我有这些条目50000,我试图去寻找食物的多少不同类型有在下面的代码中键入:
data = list(parseData("/Path/to/my/dataset/file"))
def feature(datum):
feat = [datum['food/type']]
return feat
#making a separate list of food style
foodStyle = [feature(d) for d in data]
newFoodStyle = list()
#converting the foodStyle list to just one list
for sublist in foodStyle:
for item in sublist:
newFoodStyle.append(item)
uniqueFood = Counter(newFoodStyle) #using counter variable to count how many unique food type there are
a = "There are %s types of food" % (len(uniqueFood))
print a
#print uniqueFood gives me 'Counter({'Noodle': 4352, 'Vegetable': 3412 and etc})
现在,我得到了多少有不同的食物类型, 我需要很多帮助来计算数据集中每种独特食物的“评论/味道”的平均值。
我知道有50K项,所以我想只分析最审查食物前10
我需要循环的每个条目,并查找每个uniqueFood变量,使每个uniqueFood的单独列表,例如Noodle = list []并追加以下'review/taste'编号?
任何有关如何解决这个问题的提示或想法将不胜感激。
尝试使用集合并设置长度https://docs.python.org/2/library/sets.htm升 – SatanDmytro