Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的while循环和跳过数字_Python_List_Count_While Loop - Fatal编程技术网

python中的while循环和跳过数字

python中的while循环和跳过数字,python,list,count,while-loop,Python,List,Count,While Loop,我在做一个while循环计数器,我想跳过一个特定的数字和那些数字的值+10、+20、+30…等等。以下是我到目前为止一直在尝试的: lstLength = 26 skipNum = [2, 8] lst = [] count = 0 while len(lst) < lstLength: count +=1 if count == skipNum[0] or count == skipNum[1]: continue lst.append(coun

我在做一个while循环计数器,我想跳过一个特定的数字和那些数字的值+10、+20、+30…等等。以下是我到目前为止一直在尝试的:

lstLength = 26
skipNum = [2, 8]

lst = []
count = 0
while len(lst) < lstLength:
    count +=1
    if count == skipNum[0] or count == skipNum[1]:
        continue
    lst.append(count)
print(lst)
我想让结果列表的长度变长,同时跳过数字2、8、12、18、22、28、32、38等等……我该怎么做?谢谢大家!

使用:

if count % 10 in skipNum:
    continue
%是模数运算符,因此计数%10是计数的最后一位。

使用:

if count % 10 in skipNum:
    continue

%是模数运算符,因此计数%10是计数的最后一位。

以下是我的尝试。它将填充您的列表,直到其长度达到25。它将使用skipNum中的值忽略值2、8以及2+10、2+20、。。。和8+10,8+20

lstLength = 26
skipNum = [2, 8]

count = 0
lst = []
while len(lst) < lstLength:
  count += 1

  if count in skipNum:
    continue

  #check if count is equal to 2+10, 2+20, ... or 8+10, 8+20, ... etc. If
  #that's the case, `add` will be `False`

  #map to a list of booleans indicating whether `count = skipNum+(n*10)` for any `n`
  bool_arr = map(lambda x, y: ((count - y) % 10 != 0), skipNum)

  #reduce array of booleans to a single boolean value indicating we can `add`
  add = not any(bool_arr)

  if add:
    lst.append(count)


print(lst)

这是我的尝试。它将填充您的列表,直到其长度达到25。它将使用skipNum中的值忽略值2、8以及2+10、2+20、。。。和8+10,8+20

lstLength = 26
skipNum = [2, 8]

count = 0
lst = []
while len(lst) < lstLength:
  count += 1

  if count in skipNum:
    continue

  #check if count is equal to 2+10, 2+20, ... or 8+10, 8+20, ... etc. If
  #that's the case, `add` will be `False`

  #map to a list of booleans indicating whether `count = skipNum+(n*10)` for any `n`
  bool_arr = map(lambda x, y: ((count - y) % 10 != 0), skipNum)

  #reduce array of booleans to a single boolean value indicating we can `add`
  add = not any(bool_arr)

  if add:
    lst.append(count)


print(lst)

为了比较起见,这里有一种使用itertools函数实现此功能的方法,它比while循环方法更快:

from itertools import count, ifilter, islice

def iter_filt(black_list, lstLen):
    return list(islice(ifilter(lambda x: x % 10 not in black_list, count(1)), lstLen))
用法:

>> print(iter_filt([2,8], 26))
[1, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 17, 18, 19, 20, 21, 23, 24, 25, 27, 28, 29, 30, 31, 33]
该函数的工作原理是从count1开始,它将生成从1 1,2,3,4,5,6开始的无限系列数字,。。。。然后它通过ifilter运行,ifilter过滤掉Barmar建议的x%10在黑名单中的任何结果。然后,islice仅用于从ifiltered count迭代器获取第一个lstLen结果

这样做比while循环方法快一点:

from itertools import count, islice, ifilter
import timeit

def while_filt(skipNum, lstLength):
    lst = []
    count_ = 0 
    while len(lst) < lstLength:
        count_ +=1 
        if count_ % 10 in skipNum:
            continue
        lst.append(count_)
    return lst 



def iter_filt(black_list, lstLen):
    return list(islice(ifilter(lambda x: x % 10 not in black_list, count(1)), lstLen))


if __name__ == "__main__":
    black_list = [2,6]
    length = 1000026
    print timeit.timeit("while_filt(%s,%s)" % (black_list, length),
                  setup="from __main__ import while_filt", number=50)
    print timeit.timeit("iter_filt(%s,%s)" % (black_list, length),
                  setup="from __main__ import iter_filt", number=50)

为了比较起见,这里有一种使用itertools函数实现此功能的方法,它比while循环方法更快:

from itertools import count, ifilter, islice

def iter_filt(black_list, lstLen):
    return list(islice(ifilter(lambda x: x % 10 not in black_list, count(1)), lstLen))
用法:

>> print(iter_filt([2,8], 26))
[1, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 17, 18, 19, 20, 21, 23, 24, 25, 27, 28, 29, 30, 31, 33]
该函数的工作原理是从count1开始,它将生成从1 1,2,3,4,5,6开始的无限系列数字,。。。。然后它通过ifilter运行,ifilter过滤掉Barmar建议的x%10在黑名单中的任何结果。然后,islice仅用于从ifiltered count迭代器获取第一个lstLen结果

这样做比while循环方法快一点:

from itertools import count, islice, ifilter
import timeit

def while_filt(skipNum, lstLength):
    lst = []
    count_ = 0 
    while len(lst) < lstLength:
        count_ +=1 
        if count_ % 10 in skipNum:
            continue
        lst.append(count_)
    return lst 



def iter_filt(black_list, lstLen):
    return list(islice(ifilter(lambda x: x % 10 not in black_list, count(1)), lstLen))


if __name__ == "__main__":
    black_list = [2,6]
    length = 1000026
    print timeit.timeit("while_filt(%s,%s)" % (black_list, length),
                  setup="from __main__ import while_filt", number=50)
    print timeit.timeit("iter_filt(%s,%s)" % (black_list, length),
                  setup="from __main__ import iter_filt", number=50)

当然,等我回家后再试试。@Barmar reduce的文档在这里:。基本上,它将lambda函数应用于skipNum中的每个元素,使用前面的结果作为x参数,并在第一次迭代中使用第三个参数True作为x参数。因此,基本上,如果skipNum中的任一元素的count-y%10==0,reduce调用将返回False,并且该项不会添加到列表中。这是可行的,但最终比您的解决方案慢得多。我知道reduce的功能,但这是它的一个不寻常的用法,这就是为什么我要求您对答案进行解释。@Barmar:我将reduce分为两个操作,以使其更清晰。当然,我稍后回家时会尝试。@Barmar reduce的文档在这里:。基本上,它将lambda函数应用于skipNum中的每个元素,使用前面的结果作为x参数,并在第一次迭代中使用第三个参数True作为x参数。因此,基本上,如果skipNum中的任一元素的count-y%10==0,reduce调用将返回False,并且该项不会添加到列表中。这是可行的,但最终比您的解决方案慢得多。我知道reduce的作用,但这是它的一个不寻常的用法,这就是为什么我要求您对答案添加一个解释。@Barmar:我将reduce分为两个操作,以使它更清楚一些。