gpt4 book ai didi

python - 计算并发事件的数量

转载 作者:行者123 更新时间:2023-12-01 01:48:25 24 4
gpt4 key购买 nike

此问题与此处链接的上一个问题相关 Number of consecutive values above certain cut off稍微与@kvantour提供的答案

所以就我而言,我有这样的文件(示例输入)

 0.34
0.3432
0.32
0.35
0.323
0.3623
0.345
0.32
0.31
0.378
0.34
0.35
0.342

首先:在我的例子中,如果 2 个或更多连续值满足给定的截止值 c =0.33,我将其称为 B。这是由条件 m 给出的,请参见此处:Number of consecutive values above certain cut off

第二:如果 c 是 0.33 并且我想要低于 c 的值,它将打印出 0.32 和 0.323,但我也想打印出 0.35,因为这只是 c 上方的一个出现,不符合我的规则m 中提到,因此它也会被打印出来。 @kvantour 回答了这个问题。我称之为F

所以在我的 $2 中,各州看起来像这样 BCBCB

因此我可以计算损坏的 (B) 事件的数量,如下所示 (示例输出)

2 #this corresponds to no. of frames in first B 
2 #this corresponds to no. of frames in second B
4 #this corresponds to no. of frames in third B

有没有办法计算并打印出数量。损坏的事件作为列表?

最佳答案

awk 来救援(与 friend 一起)

$ awk 'NR>1{print ($2>0.33)}' file | uniq -c | awk '$2 && $1>1{print $1}'

2
2
4

说明

NR>1{print ($2>0.33)} print 1 or 0 whether the condition $2>0.33 is satisfied, skipping the first header line.

uniq -c count the consecutive duplicate values (chain length)

$2 && $1>1{print $1} print the chain length when the value is nonzero (here it's 1) and length is greater than one as specified by OP.

关于python - 计算并发事件的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50983763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com