Python 如何将一个巨大的列表划分为相等的块,并对这些块应用条件?
分为两部分 第一部分 我有一个包含数字的列表(包括+ve和-ve) 这些数字基本上是基于15分钟的时间段,因此一天有96个街区,一年有35040个街区 我有一年的数据,所以列表中有35040个值。我想按天(96个区块)划分该列表。我知道如何使用收益率函数划分列表,但我不知道如何访问这些列表并进行计算。第一部分是把这个列表分成96块 第二部分 假设我正在按块计算违规的数量,那么我希望我的计数器在第97块重置,这意味着每天都会有一个新的计数器 我的代码用于划分列表: 我不知道如何在列表中的第97个街区重置计数器。基本上,我想按天提取数据,96个值代表1天,有35040个值,因此将有365天。因此,请每天帮助提取数据并重置计数器 预期产出:Python 如何将一个巨大的列表划分为相等的块,并对这些块应用条件?,python,python-3.x,pandas,list,python-2.7,Python,Python 3.x,Pandas,List,Python 2.7,分为两部分 第一部分 我有一个包含数字的列表(包括+ve和-ve) 这些数字基本上是基于15分钟的时间段,因此一天有96个街区,一年有35040个街区 我有一年的数据,所以列表中有35040个值。我想按天(96个区块)划分该列表。我知道如何使用收益率函数划分列表,但我不知道如何访问这些列表并进行计算。第一部分是把这个列表分成96块 第二部分 假设我正在按块计算违规的数量,那么我希望我的计数器在第97块重置,这意味着每天都会有一个新的计数器 我的代码用于划分列表: 我不知道如何在列表中的第97个街
time interval(15min) days dev counter
1 -1
2 -2
3 -54
4 -21
5 -42
6 -11
7 -32 1
. .
. .
. 31 13
96 1 32 (counter reset)
97 84
98 32
99 12
100 11
101 1
102 3
103 23 1
.
191 12
192 2 -43 (counter reset)
...
35040 365 -54
我的柜台代码:
for each in dev:
if each > 0:
minus_counter = 0
plus_counter += 1
if plus_counter == 7:
count = answer_counter
row_counter = answer_counter
counts.append(count)
plus_counter = 0
answer_counter += 1
else:
counts.append(0)
elif each < 0:
plus_counter = 0
minus_counter += 1
if minus_counter == 7:
count = answer_counter
row_counter = answer_counter
counts.append(count)
minus_counter = 0
answer_counter += 1
else:
counts.append(0)
row_counter += 1
对于dev中的每个:
如果每个>0:
负计数器=0
加号计数器+=1
如果加号计数器==7:
计数=应答计数器
行计数器=应答计数器
counts.append(count)
加号计数器=0
回答:计数器+=1
其他:
计数。追加(0)
elif均<0:
加号计数器=0
负_计数器+=1
如果负_计数器==7:
计数=应答计数器
行计数器=应答计数器
counts.append(count)
负计数器=0
回答:计数器+=1
其他:
计数。追加(0)
行计数器+=1
这将计算7个连续的-ve或+ve dev的数量。
休息日重置部分我不知道怎么做。在对数据进行分区后,按天计算-并对分区进行枚举,从枚举中获取当天的数字 例如:
- 创建负数/正数并对其求值
- 计算5以上的正/负/量级数量
显示预期结果使用计数器和if条件测试它什么是
违规
?您的列表是什么样子的?我编辑了我的问题并添加了预期结果和我的尝试
for each in dev:
if each > 0:
minus_counter = 0
plus_counter += 1
if plus_counter == 7:
count = answer_counter
row_counter = answer_counter
counts.append(count)
plus_counter = 0
answer_counter += 1
else:
counts.append(0)
elif each < 0:
plus_counter = 0
minus_counter += 1
if minus_counter == 7:
count = answer_counter
row_counter = answer_counter
counts.append(count)
minus_counter = 0
answer_counter += 1
else:
counts.append(0)
row_counter += 1
import random
data = [random.randint(-5,5)/.7 for _ in range(28)]
# partition into days with 4 datapoints per day
days = [ data[i:i+4] for i in range(0,28,4)]
# iterate over day data - no need to reset counters
# create tuples for each day, counting whatever we want)
# violations could be, f.e. magnitude > 5
count_em = [ (sum(v < 0 for v in d), sum(v >= 0 for v in d),
sum(abs(v) > 5 for v in d)) for d in days ]
# zip the data and the countings together, enumerate and output them
for day,(d,cnt) in enumerate(zip(days,count_em),1):
print("day:",day, d)
print("negative: {} positive/zero: {} magnitude > 5: {}\n".format(*cnt))
# total magnitudes > 5:
# print ( sum( c[2] for c in count_em))
day: 1 [5.714285714285714, 5.714285714285714, -1.4285714285714286, -5.714285714285714]
negative: 2 positive/zero: 2 magnitude > 5: 3
day: 2 [0.0, 7.142857142857143, 2.857142857142857, -4.285714285714286]
negative: 1 positive/zero: 3 magnitude > 5: 1
day: 3 [7.142857142857143, -7.142857142857143, 5.714285714285714, 0.0]
negative: 1 positive/zero: 3 magnitude > 5: 3
day: 4 [-5.714285714285714, 5.714285714285714, 0.0, 2.857142857142857]
negative: 1 positive/zero: 3 magnitude > 5: 2
day: 5 [-4.285714285714286, -1.4285714285714286, -1.4285714285714286, 1.4285714285714286]
negative: 3 positive/zero: 1 magnitude > 5: 0
day: 6 [4.285714285714286, 0.0, -2.857142857142857, -1.4285714285714286]
negative: 2 positive/zero: 2 magnitude > 5: 0
day: 7 [1.4285714285714286, 4.285714285714286, -2.857142857142857, -4.285714285714286]
negative: 2 positive/zero: 2 magnitude > 5: 0