在python中制作字符串值的直方图 [英] Making a histogram of string values in python

查看:107
本文介绍了在python中制作字符串值的直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有六个可能的数据值,分别是'32','22','12','31','21'和'11'.我将这些存储为字符串. python是否可以对数据进行排序,然后仅创建六个bin并显示我有多少个bin?还是直方图的输入必须是数字的?

OK so I have six possible values for data to be which are '32', '22', '12', '31', '21' and '11'. I have these stored as strings. Is it possible for python to sort through the data and just make six bins and show how many of each I have? Or do the inputs to a histogram HAVE to be numerical?

推荐答案

data =  ['32', '22', '12', '32', '22', '12', '31', '21', '11']
dict((x, data.count(x)) for x in data)

结果

{'11': 1, '12': 2, '21': 1, '22': 2, '31': 1, '32': 2}

这篇关于在python中制作字符串值的直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆