简体   繁体   English

如何在python中检查一个数字的所有数字是否都是奇数?

[英]How to check if all the digits of a number are odd in python?

I was told to solve a problem in which I would have to find out the number of 4-digit numbers which are all composed of odd digits.有人告诉我要解决一个问题,我必须找出所有由奇数组成的 4 位数字的数量。 I tried the following python code:我尝试了以下python代码:

new_list =[] # A list which holds the numbers
for a in range(1111,10000):
    for b in str(a):
        if int(b) % 2 == 1:
            new_list.append(a)
print len(new_list)

However, it has a flaw in the second loop's if statement which only checks if the current digit is odd and it also caused the digits to be added multiple times, not to mention they didn't meet the criteria.但是,它在第​​二个循环的 if 语句中存在缺陷,该语句仅检查当前数字是否为奇数,并且还导致数字被多次添加,更不用说它们不符合标准。 So how do I solve this problem using python?那么如何使用python解决这个问题呢? And if you could write a function that checks if all the numbers of a list(argument) are composed of numbers which consist of only odd/even digits, it would be great too.如果您可以编写一个函数来检查列表(参数)的所有数字是否由仅由奇数/偶数数字组成的数字组成,那也很棒。

Edit: I also need a list of the numbers, that's why multiplication can't do all of the work.编辑:我还需要一个数字列表,这就是为什么乘法不能完成所有工作。

A 4 digit number that is composed only of odd digits can only use the digits 1 , 3 , 5 , 7 and 9 .即只的奇数数字只能使用数字组成的4位的数字13579 That gives you 5 to the power 4 is 625 different numbers.这给你 5 的 4 次方是 625 个不同的数字。 That didn't require trying them all out.这不需要全部尝试。

You can still do that of course, using itertools.product() for example:当然,您仍然可以这样做,例如使用itertools.product()

from itertools import product

print sum(1 for combo in product('13579', repeat=4))

because product('13579', repeat=4) will produce all possible combinations of 4 characters from the string of odd digits.因为product('13579', repeat=4)将从奇数字符串中生成 4 个字符的所有可能组合。

Your code needs to test if all digits are odd;您的代码需要测试所有数字是否都是奇数; break out early if any digit is not odd:如果任何数字不是奇数,则提前突破:

new_list =[] # A list which holds the numbers
for a in range(1111,10000):
    for b in str(a):
        if int(b) % 2 == 0:
            break
    else:
        # only executed if the loop wasn't broken out of
        new_list.append(a)

You could use the all() function with a generator expression for that test too:您也可以将all()函数与生成器表达式一起用于该测试:

new_list = []
for a in range(1111,10000):
    if all(int(b) % 2 == 1 for b in str(a)):
        new_list.append(a)

which then can be collapsed into a list comprehension:然后可以将其折叠为列表理解:

new_list = [a for a in range(1111,10000) if all(int(b) % 2 == 1 for b in str(a))]

I believe something like this would work if you're looking to model what you already have, but I echo the comment by yotommy that some intuitive multiplication would do the trick.我相信,如果您想对已有的东西进行建模,这样的事情会起作用,但我赞同 yotommy 的评论,即一些直观的乘法可以解决问题。

 for a in range(1111,10000):
       allOdd = True
       for b in str(a):
           if int(b) % 2 == 0:
               allOdd = False
       if(allOdd):
          new_list.append(a)

You could try something like this你可以试试这样的

def isAllOdd(num):
  if num < 10: return num % 2 == 1;
  return isAllOdd(num % 10) and isAllOdd(int(num / 10))
for i in range(1000,3001):
  s=str(i)
  if (int(s[0])%2==0 and int(s[1])%2==0 and int(s[2])%2==0 and int(s[3])%2==0):
      print(i,end=",")

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

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