Python 打印列表中的所有偶数,直到给定的数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14537063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print all even numbers in a list until a given number
提问by Athoxx
I am just beginning to dabble in Python, and have started to go through the chapters on learnpython.org. In the 'Loops' chapter, I have solved the challenge with the following code. However I am not sure it is the most efficient. It certainly doesn't seem to be as I have to define the "number to not go beyond" twice. In this (I'm guessing) easy problem, DRY should be possible to adhere to, right?
我刚刚开始涉足 Python,并且已经开始阅读 learnpython.org 上的章节。在“循环”一章中,我使用以下代码解决了挑战。但是我不确定它是最有效的。这当然似乎不是因为我必须两次定义“不超过的数字”。在这个(我猜)简单的问题中,DRY 应该是可以坚持的吧?
The exerciseis
该演习是
Loop through and print out all even numbers from the numbers list in the same order they are received. Don't print any numbers that come after 237 in the sequence.
循环并以接收到的相同顺序从数字列表中打印出所有偶数。不要打印序列中 237 之后的任何数字。
My code:
我的代码:
numbers = [ 951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544, 615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941, 386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345, 399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217, 815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717, 958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470, 743, 527 ]
# My Solution
for x in numbers:
if x != 237:
if x % 2 == 0:
print x
if x == 237:
break
采纳答案by Eric
That's what elseand elifare for:
这就是else和elif是:
for x in numbers:
if x == 237:
break
elif x % 2 == 0:
print x
回答by Jon Clements
Another method is using itertoolswhich always comes in useful some way or another:
另一种方法是使用itertools它总是以某种方式有用:
>>> from itertools import takewhile, ifilter
>>> not_237 = takewhile(lambda L: L != 237, numbers)
>>> is_even = ifilter(lambda L: L % 2 == 0, not_237)
>>> list(is_even)
[402, 984, 360, 408, 980, 544, 390, 984, 592, 236, 942, 386, 462, 418, 344, 236, 566, 978, 328, 162, 758, 918]
So we create a lazy iterator that stops at 237, then take from that even numbers
所以我们创建了一个在 237 处停止的惰性迭代器,然后从那个偶数中取出
回答by Theodros Zelleke
This is also possible:
这也是可能的:
try:
i = numbers.index(237)
except:
i = len(numbers)
for n in numbers[:i]:
if not n%2:
print n
回答by user2906667
Im just going throug this exercise in my "learning the basis thing" and i came out with a similar but somehow worse solution, patrik :(
我只是在我的“学习基础知识”中进行了这个练习,然后我提出了一个类似但更糟糕的解决方案,patrik :(
x = 0
for loop in numbers:
if (numbers[x]) % 2 ==0:
print (numbers[x])
x = x+1
if (numbers[x]) ==237:
break
else:
x =x+1
continue
x = 0
for loop in numbers:
if (numbers[x]) % 2 ==0:
print (numbers[x])
x = x+1
if (numbers[x]) ==237:
break
else:
x =x+1
continue
回答by arthur1994
you can do that using list comprehension
你可以使用列表理解来做到这一点
The solution would be like this:
解决方案是这样的:
numbers = [x for ind,x in enumerate(numbers) if x % 2== 0 and numbers[ind+1]!=237 ]
回答by Sunil Narendra Babu
3 if's and done :)
3 如果已经完成 :)
if x % 2 == 0:
print x
if x == 237:
break
if x % 2 != 0:
continue
Output
输出
402
984
360
408
980
544
390
984
592
236
942
386
462
418
344
236
566
978
328
162
758
918

