如何在python中找到某种条件的出现频率

时间:2015-09-17 14:42:35

标签: python count

我有以下数据集:

public class ElementaryThread extends Thread
{
private SurfaceHolder   m_SurfaceHolder;
private ElementaryView  m_View;
private boolean         m_Running;

public ElementaryThread(SurfaceHolder sh, ElementaryView view)
    {
    super();
    m_SurfaceHolder = sh;
    m_View = view;
    }

public void setRunning(boolean r)       { m_Running = r; }

@SuppressLint("WrongCall") @Override
public void run() 
    {
    Canvas canvas;

    while (m_Running)
        {
        canvas =null;
        try
            {
            canvas =this.m_SurfaceHolder.lockCanvas();
            this.m_View.onDraw(canvas);
            }
        finally
            {
            if (canvas!=null)   m_SurfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }
}

我想找到checkpoint_size<的次数10240在给定数据集中是2次。我怎么能在python中实现这个?

我的代码现在是

{
    "checkpoint_size": 89453.0,
    "resource_id": "0c8b6b26",
    "timetamp": "2015-07-08T10:53:33"
}{
    "checkpoint_size": 89453.0,
    "resource_id": "d36f2527",
    "timetamp": "2015-07-08T10:53:33"
}{
    "checkpoint_size": 89453.0,
    "resource_id": "0c8b6b26",
    "timetamp": "2015-07-08T10:53:33"
}{
    "checkpoint_size": 89453.0,
    "resource_id": "d36f2527",
    "timetamp": "2015-07-08T10:43:33"
}{
    "checkpoint_size": 89453.0,
    "resource_id": "0c8b6b26",
    "timetamp": "2015-07-08T10:43:33"
}{
    "checkpoint_size": 89453.0,
    "resource_id": "d36f2527",
    "timetamp": "2015-07-08T10:43:33"
}{
    "checkpoint_size": 5000.0,
    "resource_id": "0c8b6b26",
    "timetamp": "2015-07-08T10:43:33"
}{
    "checkpoint_size": 5000.0,
    "resource_id": "d36f2527",
    "timetamp": "2015-07-08T10:33:33"
}

原始数据如下所示:

data_checkpoint_size = cclient.samples.list(meter_name ='checkpoint.size')


checkpoint_size_limit = 10240

def counterVolume(data_checkpoint_size):
  for each in data_checkpoint_size:
        x = each.counter_volume
        y = each.timestamp
        z = each.resource_id[<OldSample {u'counter_name': u'cpu_util', u'user_id': u'7bffa12f482840c7801e3e01e160c8cb', u'resource_id': u'ef392c3d-74fa-43fe-87c5-7e117b6d8a09', u'timestamp': u'2015-07-01T15:13:55', u'counter_volume': 0.034999999999999996, u'resource_metadata': {u'ramdisk_id': u'None', u'flavor.vcpus': u'1', u'OS-EXT-AZ.availability_zone': u'nova', u'display_name': u'ubuntu-io', u'flavor.id': u'596642d8-0813-4ae9-aec4-0105fdf05761', u'status': u'active', u'ephemeral_gb': u'0', u'flavor.name': u'm1.small.io', u'disk_gb': u'20', u'kernel_id': u'None', u'image.id': u'5776a360-0953-4c93-931d-a6e3616fb8dc', u'flavor.ram': u'2048', u'host': u'9fa544f5c47569db21d50bc6c0765296316a56bd6baf6b04d705686a', u'flavor.ephemeral': u'0', u'image.name': u'ubuntu-io', u'image_ref_url': u'link': u"[{'href': 'link', 'rel': 'bookmark'}]", u'cpu_number': u'1', u'flavor.disk': u'20', u'root_gb': u'20', u'name': u'instance-000000d3', u'memory_mb': u'2048', u'instance_type': u'596642d8-0813-4ae9-aec4-0105fdf05761', u'vcpus': u'1', u'image_ref': u'5776a360-0953-4c93-931d-a6e3616fb8dc', u'flavor.links': u"[{'href': 'link', 'rel': 'bookmark'}]"}, u'source': u'openstack', u'counter_unit': u'%', u'recorded_at': u'2015-07-01T15:13:56.006000', u'project_id': u'1670f0e56fb6421cb83d81b60b149c04', u'message_id': u'ca8ea466-2003-11e5-a764-002590e64886', u'counter_type': u'gauge'}>, <OldSample {u'counter_name': u'cpu_util', u'user_id': u'7bffa12f482840c7801e3e01e160c8cb', u'resource_id': u'0c8b6b26-3340-41e3-ac8b-cc38f15d3570', u'timestamp': u'2015-07-01T15:08:32', u'counter_volume': 5.4399999999999995, u'resource_metadata': {u'ramdisk_id': u'None', u'flavor.vcpus': u'1', u'OS-EXT-AZ.availability_zone': u'nova', u'display_name': u'kalman_instance', u'flavor.id': u'1', u'status': u'active', u'ephemeral_gb': u'0', u'flavor.name': u'm1.tiny', u'disk_gb': u'1', u'kernel_id': u'None', u'image.id': u'1c9b08f0-d1fa-4acc-a11c-87b77310158c', u'flavor.ram': u'512', u'host': u'25aa71ded460ea9d4bf52e1aac34017691699cb5e4e389704d738bed', u'flavor.ephemeral': u'0', u'image.name': u'cirros', u'image_ref_url': u'http://192.168.26.1:8774/d1d65b6feab741a6a2905e6197cb15ee/images/1c9b08f0-d1fa-4acc-a11c-87b77310158c', u'image.links': u"[{'href': 'http://192.168.26.1:8774/d1d65b6feab741a6a2905e6197cb15ee/images/1c9b08f0-d1fa-4acc-a11c-87b77310158c', 'rel': 'bookmark'}]", u'cpu_number': u'1', u'flavor.disk': u'1', u'root_gb': u'1', u'name': u'instance-0000013c', u'memory_mb': u'512', u'instance_type': u'1', u'vcpus': u'1', u'image_ref': u'1c9b08f0-d1fa-4acc-a11c-87b77310158c', u'flavor.links': u"[{'href': 'http://192.168.26.1:8774/d1d65b6feab741a6a2905e6197cb15ee/flavors/1', 'rel': 'bookmark'}]"}, u'source': u'openstack', u'counter_unit': u'%', u'recorded_at': u'2015-07-01T15:08:32.459000', u'project_id': u'1670f0e56fb6421cb83d81b60b149c04', u'message_id': u'09b5173e-2003-11e5-ac7a-002590e64b12', u'counter_type': u'gauge'}>]
        with open('datae.txt', 'a') as outfile:
          json.dump({'checkpoint_size': x, 'timetamp': y, 'resource_id': z}, outfile, sort_keys=True, indent=4, separators=(',', ': '))

3 个答案:

答案 0 :(得分:2)

对您的功能进行一些更正:

def counterVolume(data_checkpoint_size):
    counter=0 
    for each in data_checkpoint_size:
        if each["checkpoint_size"]<checkpoint_size_limit:
            counter+=1
    return counter

答案 1 :(得分:0)

以下是包含summaplambda

的更简单版本
sum( 
    map(lambda data, checkpoint_size_limit=10000: 
            1 if data['checkpoint_size']<checkpoint_size_limit else 0
        , data_checkpoint_size
       ) 
)
  • lambda函数检查条件,如果条件为真则返回1,否则返回0.
  • map将给定的函数应用于列表的所有元素(iterable)。
  • sum这给出了条件为真的变量计数。

答案 2 :(得分:0)

简单的解决方案:

sum(d['checkpoint_size'] < checkpoint_size_limit for d in data_checkpoint_size)

汇总布尔值列表。在转换为数字期间,True被解析为1,False被解析为0。