使用 Python 模运算符对列表进行排序

发布于 2024-11-17 17:50:58 字数 762 浏览 7 评论 0原文

我一直在研究欧拉项目问题来尝试学习Python,并且我编写了第二个问题的解决方案(找到斐波那契数列中不超过四百万的偶数项的总和)。该代码为我提供了正确的解决方案,但它要求我使用两次模除法,以便从我生成的斐波那契数列表中删除奇数值。这是我写的解决方案:

term_1 = 1
term_2 = 2
fibonacci_list = [1]
while term_2 < 4000000:
    fibonacci_list.append(term_2)
    term_1, term_2 = term_2, term_1 + term_2
for num in fibonacci_list:
    if num % 2 != 0
        fibonacci_list.remove(num)
for num in fibonacci_list:
    if num % 2 != 0
        fibonacci_list.remove(num)
return sum(fibonacci_list)

如果我只放入一个 for 循环,则 fibonacci_list 列表将变为以下内容:

[2, 5, 8, 21, 34, 89, 144, 377, 610, 1597, 2584, 6765, 10946, 28657, 46368, 121393, 196418, 514229, 832040, 2178309, 3524578]

难道所有奇数项都不能通过模数除法测试并被删除吗?为什么我需要运行 for 循环两次才能删除所有奇数项?

I've been working on the Project Euler problems to try and learn python and I wrote a solution to the second problem (find the sum of even-valued terms in the Fibonacci sequence that do not exceed four million). The code gives me the correct solution, but it requires me to use modulus division twice in order to remove the odd-numbered values from the list of fibonacci numbers I generated. Here is the solution I wrote:

term_1 = 1
term_2 = 2
fibonacci_list = [1]
while term_2 < 4000000:
    fibonacci_list.append(term_2)
    term_1, term_2 = term_2, term_1 + term_2
for num in fibonacci_list:
    if num % 2 != 0
        fibonacci_list.remove(num)
for num in fibonacci_list:
    if num % 2 != 0
        fibonacci_list.remove(num)
return sum(fibonacci_list)

If I only put in one for-loop, the list fibonacci_list becomes the following:

[2, 5, 8, 21, 34, 89, 144, 377, 610, 1597, 2584, 6765, 10946, 28657, 46368, 121393, 196418, 514229, 832040, 2178309, 3524578]

Shouldn't all the odd numbered terms fail the modulus division test and be removed? Why do I need to run the for loop twice to remove all odd numbered terms?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

顾铮苏瑾 2024-11-24 17:50:58

我想您面临的问题是您在迭代列表时尝试从列表中删除项目。

请参阅此处此处此处了解有关同一主题的先前问题。

为了便于讨论,我们假设这实际上是问题所在,并且假设在迭代列表时禁止从列表中删除项目。

您可以采取哪些不同的做法,从而使您在迭代列表时无需从列表中删除项目?我不确定你是否想直接得到答案,因为你正在做欧拉计划,所以我不会给出任何明显的答案。

I imagine the problem you are facing is that you are attempting to remove items from a list while iterating over the list.

See here, here and here for previous questions on this same topic.

For the sake of discussion, let's assume this is in fact the problem, and let's assume that it is forbidden to remove items from a list while iterating over it.

What could you do differently that would result in you not needing to remove items from a list while iterating over it? I'm not sure if you want to be given the answer outright or not since you are doing Project Euler, so I will refrain from giving out any obvious answers.

爱的那么颓废 2024-11-24 17:50:58

只简单地看了一下,但看起来您正在改变正在迭代的集合,即当您删除项目时,指向当前项目/下一个项目的指针将受到影响,并且某些项目可能会在第一次通过时被跳过。

Only looked at this briefly but it looks like you are mutating the collection that you are iterating through, i.e. as you remove items, the pointer to the current item/next item will be affected and certain items may be skipped on the first pass through.

输什么也不输骨气 2024-11-24 17:50:58

不难看出,斐波那契数列中只有每第三项都是偶数。你可以用它来代替。

无论如何,您陷入了变异的经典陷阱迭代它时的序列。不要这样做,请这样做:

fibonacci_list[:] = [x for x in fibonacci_list if x%2==0]

It's not hard to see that only every 3rd term of the fibonacci sequence is even. You could use that instead.

In any case, you fell into the classic trap of mutating a sequence while iterating over it. Don't do that, do this:

fibonacci_list[:] = [x for x in fibonacci_list if x%2==0]
睫毛溺水了 2024-11-24 17:50:58

将您的程序与此进行比较。这可能有帮助。

fibonacci = [1,2]
num = 3
while num < 4000000:
    fibonacci.append(num)
    len_ = len(fibonacci)
    num = fibonacci[len_-2] + fibonacci[len_-1]

sum = 0
for num in fibonacci:
    if num%2 == 0: sum += num

print sum

我不明白为什么你没有必要从列表中删除奇数条目。只需添加偶数即可。

Compare your program to this. It might help.

fibonacci = [1,2]
num = 3
while num < 4000000:
    fibonacci.append(num)
    len_ = len(fibonacci)
    num = fibonacci[len_-2] + fibonacci[len_-1]

sum = 0
for num in fibonacci:
    if num%2 == 0: sum += num

print sum

I don't understand why are you unnecessary removing the odd numbered entries from the list. Just add the even numbered ones that's it.

小忆控 2024-11-24 17:50:58

这让我想起了埃拉托色尼的筛子。所以我想提出这个解决方案,假设将您的列表转换为数组:

fibonacci_list = fibonacci_list [ fibonacci_list % 2 != 0  ]

This remembers me the sieve of Eratosthenes. So I would like to propose this solution, which suppose converting your list to an array:

fibonacci_list = fibonacci_list [ fibonacci_list % 2 != 0  ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文