我有大量数据,因此我试图确定数据的多少行包含“播放”和“ 0”。这被列为“ count_of_plate_appearances”,但是,我不确定如何只打印最终值,而不是所有值。我觉得似乎可能对此有一个直接的答案,但是,我对python很陌生。任何帮助,将不胜感激。
for j in range(i+46,i+150,1):
temp_array2 = lines[j].rstrip().split(",")
if temp_array2[0] == "play" and temp_array2[2] == "1":
count_of_plate_appearances=count_of_plate_appearances+1
print(count_of_plate_appearances)
输出为
1
2
3
4
5
6
7
8
1
2
3
4
5
6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
但是,我希望输出为
8
6
15
7
非常感谢!
答案 0 :(得分:4)
您要在迭代每个j之后打印结果。我认为您应该在迭代每个i之后打印结果。
尝试一下
for j in range(i+46,i+150,1):
temp_array2 = lines[j].rstrip().split(",")
if temp_array2[0] == "play" and temp_array2[2] == "1":
count_of_plate_appearances=count_of_plate_appearances+1
print(count_of_plate_appearances)
从何处开始打印。
答案 1 :(得分:0)
for i in range(4):
count_of_plate_appearances=0
for j in range(i+46,i+150,1):
temp_array2 = lines[j].rstrip().split(",")
if temp_array2[0] == "play" and temp_array2[2] == "1":
count_of_plate_appearances=count_of_plate_appearances+1
print(count_of_plate_appearances)