列表中连续零的计数 [英] Count of consecutive zeros in a list

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

问题描述

我试图编写一个通过遍历列表并具有counter = 0来查找此函数的函数,当找到0时counter + = 1,并且当找到第一个非0值时,将启动一个新计数器,接下来的0在列表中将添加到新的计数器,等等. 但是,这不是教授希望如何计算的方式,但是正如我之前提到的,我错过了一个课程,并且正在努力了解如何使用flags/boolean代替

I have tried to write a function to find this by looping through my list and having a counter=0 and when a 0 is found counter+=1 and when the first non 0 is found a new counter would start and the next 0 in the list would add to the new counter, etc.. However, this is not how my prof wants this calculated but as I mentioned earlier I missed a class and am struggling to understand how to use flags/boolean instead

我的程序必须:

  1. 提示用户输入列表的大小(N).然后提示用户输入N个数字,并将其存储到名为BurstList的列表中.

  1. Prompt the user for the size of the list (N). Then prompt the user to enter N numbers and store them into a list called burstList.

鉴于N和burstList,您的程序应计算零脉冲串的长度,并将其存储在名为BurstLengths的列表中.

Given N and burstList, your program should compute the lengths of the bursts of zeros and store them in a list called burstLengths.

给出列表burstLength,使用while循环打印突发长度列表.

Given list burstLengths, use a while loop to print the list of burst lengths.

我的教授希望我们合并标志和布尔数据类型.使用循环遍历列表,一旦找到0,将标志设置为true,并使用计数器递增.使用列表,以后您可以将计数器添加到每个突发"

My professor wants us to "Incorporate flags and boolean data types. Use a loop to go through the list and once a 0 is located, set the flag to true and use a counter to increment. Use a list that later you could append the counter to for each burst"

让我说:
N = 15
BurstList = [1、0、0、0、0、3、7、0、0、0、0、0、0、5、0]

Lets say I have:
N=15
burstList=[1, 0, 0, 0, 0, 3, 7, 0, 0, 0, 0, 0, 0, 5, 0]

然后,我的列表burstLengths应该包含:
[4,6,1]

Then my list burstLengths should contain:
[4, 6, 1]

到目前为止,我所拥有的只是:

So far all I have is:

burstList = []
N = int(input("Enter the length of the list: "))

def listSize():
>for i in range(N):
>>value = int(input("Enter integer value "+str(i+1)+": "))
>>burstList.append(value)
>print("List = ", burstList)

推荐答案

您的作业大约需要10-20行代码.简而言之,您应该做的是:

Your assignment will take roughly 10-20 lines of code. In a nutshell, what you are expected to do is:

  1. 初始化counterflag. counter将对突发计数,而flag将让您知道何时进行计数.
  2. 对于burstList中的每个数字x
    • 检查x是否为0.如果是,请将flag设置为True并增加counter.
    • 如果x不等于零,则检查flag是否仍为True.如果是,这意味着您需要将counter的当前值保存在burstLengths内,将其重置,然后将flag设置为False.
  1. Initialise a counter and a flag. counter will count bursts, and flag will let you know when to count.
  2. For each number x in burstList,
    • check if x is a 0. If yes, set flag to True and increment your counter.
    • if x is not equal to zero, then check whether flag is still True. If it is, that means you will need to save the current value of counter inside burstLengths, reset it, and set flag to False.

最后,您需要注意以下事实:在迭代之后,您可能尚未注册最后一个突发(如果有的话)(例如,如果burstList的最后一个k元素为0).因此,您将不得不处理该问题.

Finally, you will need to take care of the fact that, after iterating, you may not have registered the last burst, if any (for example, if the last k elements of burstList is 0). So you will have to handle that.

由于这是一项任务,因此以下是您无法上交的代码的解决方案,仅用于演示:-)

Since this is an assignment, here's the solution using code that you can't turn in, just for demonstration :-)

import pandas as pd
v = pd.Series(burstList).eq(0)
_, burstLengths = pd.np.unique(
    v.ne(v.shift()).cumsum().where(v).dropna(), return_counts=True)

print(burstLengths.tolist())
[4, 6, 1]


或者,如果您希望获得一些想法,请参考以下入门代码,其中缺少一些基本内容:


Or, if you'd like some idea, here's some starter code with the essential pieces missing:

counter = 0
flag = False
burstLengths = []
for i in burstList:
   if i == 0:
       ???
   else:
       ???

if flag:
    ???

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

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