简体   繁体   English

如何创建一个不被 3 整除的 5 个下一个数字的列表

[英]How to create a list of 5 next numbers that are not divided by 3

dear community, I started learning Python and want to create a list of 5 next (after the given number) numbers that are not divided by 3 (modulo).亲爱的社区,我开始学习 Python 并希望创建一个不除以 3(模数)的 5 个下一个(在给定数字之后)数字的列表。 Here is my code:这是我的代码:

def main(N):
    
#print a list of 5 next numbers that are not divided by 3
    l = list()
    for i in range(int(N), 150):
        if len(l) < 5:
            if i%3==0:
                l.append(i+1)
    return [l]

at the end I get fe for the print(main(99)) :最后我得到 fe 的print(main(99))

>>> [100, 103, 106, 109, 112]

I also tried to put i+=1 after if statement but in the end I got even weirder result:我还尝试将 i+=1 放在 if 语句之后,但最后我得到了更奇怪的结果:

[101, 101, 102, 104, 104]

What do I do wrong and how can I fix it?我做错了什么,我该如何解决? Thanks!谢谢!

I would write a generator that yields numbers not divisible by 3 with a base number and count as arguments:我会编写一个生成器,它产生不能被 3 整除的数字,其基数为 arguments:

def three_no_factor(n, cnt):
    x = 1
    while x <= cnt:
        if n % 3 != 0:
            yield n
            x += 1
        n += 1

Then use that to get the list of 5 numbers not divisible by 3:然后用它来获取不能被 3 整除的 5 个数字的列表:

>>> [x for x in three_no_factor(100,5)]
[100, 101, 103, 104, 106]

Unless there's a specific reason to cap the values at 150, you can just let the loop run until the list is full.除非有特定原因将值限制为 150,否则您可以让循环运行直到列表已满。

def next_nontriples(N):
# return a list of 5 next numbers that are not divisible by 3
    ls = list()
    nx = N + 1
    while len(ls) < 5:
        if nx%3 != 0:
            ls.append(nx)
        nx += 1
    return ls

Using the != operator is the key.使用!=运算符是关键。 Note that I'm returning the resulting list, whereas you return a list that has the required list as its only element, due to the extra square brackets around your return item.请注意,我返回的是结果列表,而您返回的列表具有所需的列表作为其唯一元素,因为您的return项目周围有额外的方括号。

You can fill the list once per loop-5-times too:您也可以每循环 5 次填写一次列表:

def next_nontriples(N):
# return a list of 5 next numbers that are not divisible by 3
    ls = list()
    nx = N + 1
    for ix in range(5):
        if nx%3 == 0:
            nx += 1 # skip this one 
        ls.append(nx)
        nx += 1
    return ls

Here we just add an increment to our candidate value if it fails the test (by being divisible by three).在这里,如果它未通过测试(通过被三整除),我们只是为我们的候选值添加一个增量。 We know that the next number will qualify.我们知道下一个数字将符合条件。

Finally you could just handle the three cases separately.最后,您可以分别处理这三种情况。 Not recommended particularly here but hard-coding is sometimes your friend:这里不特别推荐,但硬编码有时是你的朋友:

def next_nontriples(N):
# return a list of 5 next numbers that are not divisible by 3
    if N%3 == 0:
        return [N+1, N+2, N+4, N+5, N+7]
    if N%3 == 1:
        return [N+1, N+3, N+4, N+6, N+7]
    if N%3 == 2:
        return [N+2, N+3, N+5, N+6, N+8]

def main(N):
    
#print a list of 5 next numbers that are not divided by 3
    l = list()
    i = N + 1
    while len(l)<5:
        if i%3!=0:
            l.append(i)
        i += 1
    return [l]

Another way - >另一种方式->

def main(N):
    gen = (i for i in range(int(N), 150) if i % 3 != 0)  # create generator
    return [next(gen) for j in range(5)]  # iterate 5 times


main(99) # [100, 101, 103, 104, 106]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Odoo中创建可编辑的列表视图分为不同的月份? - How to create editable list view divided into different number of months in Odoo? 如何创建数字列表 - How to create a list of numbers 如何创建一个脚本,以查找可以被4除的range(0,100)中的所有数字? - How to create a script that will find all numbers in range(0,100) that can be divided by 4? 如何使用Python中的过滤器功能获取除以某个数字的数字列表? - How to obtain a list of numbers that are divided by a certain number using the filter function in Python? 如何检查数字是否可以被两个数字整除 - How to check if number can be divided by two numbers python列表理解,创建100个数字除以7的列表 - python list comperhension, creating list of 100 numbers divided by 7 创建一个大小为x的向量,其中两个数字随机划分 - Create a vector of size x with two numbers randomly divided Python 如果数字除以多个连续零,则将列表拆分为子列表 - Python split list into sublists if numbers are divided by a number of consecutive zeros 创建序列号列表,直到下一个四舍五入的数字超过x - Create list of sequential numbers until next rounded number past x 如何为Kivy中的下一个window创建下拉列表 - how to Create Dropdown list for next window in Kivy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM