查找数字的每个数字均为偶数的数字

时间:2019-06-11 04:32:24

标签: python-3.x

我正在尝试编写将包含一个范围并列出仅由偶数组成的数字(如88、202、468之类的数字)的代码。

该代码适用于三位数范围,但是如果我将范围设为(1,401)或(10,401),则它将不起作用,因为它没有三位数。

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

3 个答案:

答案 0 :(得分:1)

使用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_longest):

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

from itertools import takewhile, zip_longest

for d in digits:
    if all(i==j for i, j in zip_longest(str(d), takewhile(lambda c: int(c) % 2 == 0, str(d)))):
        print(d)

打印:

88
202
468
0

版本3(无itertools):

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

for d in digits:
    if all(int(i) / 2 == int(i) // 2 for i in str(d)):
        print(d)

打印:

88
202
468
0

答案 1 :(得分:0)

这是因为您总是尝试在此行中使用public function fields(Request $request) { $budgetProject = BudgetProject::where('budget_id',$request->budget_id)->get(); dd($budgetProject); foreach($budgetProject as $bp){ $projectArray[$bp->project_id] = $bp->project->name; } return [ Select::make('Project','project_id')->options($projectArray), ]; } 检查第3位数字:

s[2]

要使其具有通用性,请结合使用all函数和列表理解功能,例如:

if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])%2==0):

答案 2 :(得分:0)

您的错误源于无条件访问数字字符串的第三位-如果数字较短,则甚至

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

如果您想获得偶数,则可以

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

  • 检查长度== 3
  • 将数字转换为字符串
  • 对照一组偶数数字字符串检查每个字符

个数字转换回整数并使用模块化2 进行测试的成本更高,因此,将字符与一组允许的字符组合c_heck的方法要快25%(请参见下面的时间说明):< / p>

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)
print( ",".join(items))

输出:

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