Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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 3.x 查找每个数字都是偶数的数字_Python 3.x - Fatal编程技术网

Python 3.x 查找每个数字都是偶数的数字

Python 3.x 查找每个数字都是偶数的数字,python-3.x,Python 3.x,我正在尝试编写一段代码,其中包含一个范围,并列出仅由偶数组成的数字(如88202468) 该代码适用于三位数范围,但如果我设置范围(1401)或(10401),则它不起作用,因为它没有三位数 items = [] for i in range(100, 401): s = str(i) if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])%2==0): items.append(s) print( ",".

我正在尝试编写一段代码,其中包含一个范围,并列出仅由偶数组成的数字(如88202468)

该代码适用于三位数范围,但如果我设置范围(1401)或(10401),则它不起作用,因为它没有三位数

items = []
for i in range(100, 401):
    s = str(i)
    if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])%2==0):
        items.append(s)
print( ",".join(items))
当我仅将限制设置为两位数时,我会得到错误:

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])%2==0):
IndexError: string index out of range
回溯(最近一次呼叫最后一次):
文件“main.py”,第4行,在
如果(int(s[0])%2==0)和(int(s[1])%2==0)和(int(s[2])%2==0):
索引器错误:字符串索引超出范围

这是因为您总是尝试使用此行中的
s[2]
检查第三位数字:

if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])%2==0):
要使其通用,请使用具有列表理解的函数,例如:

if all(int(digit) % 2 == 0 for digit in s):

使用
itertools.takewhile

digits = [88, 202 , 468, 1024, 999, 2067, 0]

from itertools import takewhile

for d in digits:
    filtered = ''.join(takewhile(lambda c: int(c) % 2 == 0, str(d)))
    if not filtered:
        continue
    if int(filtered) == d:
        print(d)
印刷品:

88
202
468
0
版本2(使用
itertools.zip\u
):

印刷品:

88
202
468
0
88
202
468
0
第3版(不含
itertools
):

印刷品:

88
202
468
0
88
202
468
0

您的错误源于无条件地访问第三位数字的字符串,即使该数字较短

您必须检查字符串的长度==3,才能只找到3位数的解决方案

如果你想得到偶数,你可以

  • 检查长度==3
  • 将数字转换为字符串
  • 将每个数字转换回一个数字,并检查所有的模2

  • 检查长度==3
  • 将数字转换为字符串
  • 根据一组偶数数字字符串检查每个字符
将数字转换回整数并使用模2进行测试的成本更高,因此根据一组允许的字符检查字符的方法大约快25%(参见下面的计时):

输出:

200,202,204,206,208,220,222,224,226,228,240,242,244,246,248,260,262,264,266,268,280,
282,284,286,288,400,402,404,406,408,420,422,424,426,428,440,442,444,446,448,460,462,
464,466,468,480,482,484,486,488,600,602,604,606,608,620,622,624,626,628,640,642,644,
646,648,660,662,664,666,668,680,682,684,686,688,800,802,804,806,808,820,822,824,826,
828,840,842,844,846,848,860,862,864,866,868,880,882,884,886,888

集合中字符与数字mod 2==0的比较:

def with_set():
    items = []
    even = set("24680")
    for i in range(10, 1401):
        s = str(i)
        if len(s)==3 and all(c in even for c in s):
            items.append(s)
    return items

def with_mod():             
    items = []
    for i in range(10, 1401):
        s = str(i)
        if len(s)==3 and all(int(c)%2==0 for c in s):
            items.append(s)
    return items


import timeit
# try both methods 1000 times, print time
print("Set: ", timeit.timeit(with_set, number=1000))
print("Mod: ", timeit.timeit(with_mod, number=1000))
输出定时:

Set:  0.9209150870001395
Mod:  1.2408415259997128
Set:  0.9209150870001395
Mod:  1.2408415259997128