简体   繁体   English

满足条件时从列表中添加整数

[英]Add integer from list when condition is met

So if I want to determine the first 20 (not until 20) Harshard numbers how would I do this? 因此,如果我想确定前20个(直到20个)Harshard数字,我将如何做? The code I have only inputs until it reaches 20, not the first 20. 我只能输入直到20的代码,而不是前20的代码。

def isHarshad(x):
x1 = str(x)
x2 = list(x1)
x3 = [int(i) for i in x2]
x4 = sum(x3)
if x%x4 == 0:
    return True
else:
    return False

def ithHarshad(n):
return [i for i in range(1, n+1) if isHarshad(i)]

ithHarshad(20) 

Your code seems to be trying to remove the list from the integer. 您的代码似乎正在尝试从整数中删除列表。 Swap your variable names in the loop as follows: 在循环中交换变量名称,如下所示:

if not isHarshad(x):
    y1.remove(r)

I changed your condition to make it more pythonic: when comparing to Boolean values you don't have to write it explicitly. 我更改了您的条件,使其更具Python风格:与布尔值比较时,无需显式编写它。 So in this case, if isHarshad returns True, the the not inverts it and the code below is not run, but when it returns false, it is inverted to True, and the item is removed. 因此,在这种情况下,如果isHarshad返回True,则not会将其反转,并且下面的代码不运行,但是当返回false时,它将反转为True,并删除该项目。

In general, you could really shorten your code if you use a list comprehension: 通常,如果使用列表推导,您实际上可以缩短代码:

def ithHarshad(n):
    return [i for i in range(1, n+1) if isHarshad(i)]

This code means that it will create a list of the values 1 -> n (inclusive) and only include the value if the result of filtering it through isHarshad is True 此代码意味着它将创建值1 -> n (含)的列表,并且仅在通过isHarshad过滤该值的结果为True时才包含该值

You had a logical error.Just change 您有一个逻辑错误。

r.remove(y1) 

to

y1.remove(r) in function ithHarshad()

That would work ! 那行得通!

You are writing opposite in second loop 你在第二个循环中写相反的东西

for r in y1:
  if isHarshad(r) == False:
     y1.remove(r) # You wrote r.remove(y1)

y1 is your list and r is your integer in that list. y1是您的列表,r是您在该列表中的整数。 You need to remove from the list. 您需要从列表中删除。

Not the best code, but this works: 不是最好的代码,但这可行:

def isHarshad(x):
    x1 = str(x)
    x2 = list(x1)
    x3 = [int(i) for i in x2]
    x4 = sum(x3)
    if x%x4 == 0:
        return True
    else:
        return False

def ithHarshad(y):
    y1 = []
    for i in range(y):
        y1.append(i+1)
    for r in y1:
        if not isHarshad(r):
            print('r: %s; y1:%s' % (r, y1))
            y1.remove(r)
    return y1

if __name__ == "__main__":
    result = ithHarshad(13)
    print('Result for ithHarshad(13) = %s' % result

You will get a ZeroDivisionError if you divide something by zero. 如果将某物除以零,则会收到ZeroDivisionError。 In your code, the only division is happening when you use the modulo operator. 在您的代码中,唯一的除法是在使用模运算符时发生的。 If the number you pass to isHarshad() is 0, then you will get this error. 如果传递给isHarshad()为0,则将出现此错误。 Also, there is no need to convert an int to a list of characters, you can implement isHarshad() using pure integer arithmetic: 另外,不需要将int转换为字符列表,可以使用纯整数算法实现isHarshad()

def isHarshad(x):
  sum = 0
  y = x
  while y:
    sum += y % 10
    y //= 10
  return sum != 0 and x % sum == 0

Doing it this way also allows you to easily change the function to determine if a number is a Harshad number in a different base than 10. 这样,您还可以轻松更改函数,以确定数字是否是不同于10的底数的Harshad数字。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM