连续发生的项目数 [英] Count of items that occur in succession

查看:104
本文介绍了连续发生的项目数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据文件.

 1 3
 2 6
 3 7
 4 6
 5 8
 6 4
 7 5
 8 9
 9 7
 10 2
 11 3
 12 5
 13 3

我的目标是要对第2列中等于或大于5的项目进行计数,这些项目至少连续出现3次.我已经能够弄清计数部分,而不是继承部分.

My goal is to have a count of items that are equal to or greater than 5 in column 2 which appear at least 3 times in succession. I have been able to figure out the counting part but not the succession part.

因此,我希望此数据文件的输出为2,因为在第2列中有2个字符串(6,7,6,8)和(5,9,7),其中我的数字等于和大于5的至少连续出现3次.

So, I want the output of this data file to be 2 as in column 2 there are 2 strings (6,7,6,8) and (5,9,7) where I have numbers that are equal to and greater than 5 appearing at least 3 times in succession.

import numpy as np
data=np.loadtxt('/Users/Hrihaan/Desktop/DataF.txt')
z=data[:,1]
count = len([i for i in z if i >= 5])
print(count)

任何帮助将不胜感激.

推荐答案

这是使用csvitertools.groupby的纯Python方法:

Here's a pure Python approach using csv and itertools.groupby:

首先,让我伪造该文件:

First, let me fake the file:

>>> s = """1 3
... 2 6
... 3 7
... 4 6
... 5 8
... 6 4
... 7 5
... 8 9
... 9 7
... 10 2
... 11 3
... 12 5
... 13 3"""
>>> import io

现在,为了它的肉:

>>> import itertools
>>> import csv
>>> with io.StringIO(s) as f:
...     reader = csv.reader(f, delimiter=' ')
...     second_col = (int(c) for _, c in reader)
...     gb = itertools.groupby(second_col, (5).__le__)
...     x = sum(k for k, g in gb if k and len(list(g)) >= 3)
...
>>> x
2

这篇关于连续发生的项目数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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