我有这样一个清单:
dttrain = [["sunny","hot","high","false","no"],
["sunny","hot","high","true","no"],
["overcast","hot","high","false","yes"]]
我想按最后一个索引计算频率,例如:
^{pr2}$我尝试了我自己的代码:
c = Counter(x for sublist in dttrain for x in sublist)
但这表明:
Counter({'yes': 9, 'false': 8, 'high': 7, 'normal': 7, 'true': 6, 'mild': 6, 'sunny': 5, 'no': 5, 'rainy': 5, 'hot': 4,'overcast': 4, 'cool': 4})
使用
itertools(product and chain)
和collections.Counter
的组合输出:
^{pr2}$下面是一些杂乱的代码:
相关问题 更多 >
编程相关推荐