dangdangA

导航

Python活力练习Day19

Day19:给定一个整数数组nums,请你返回其中位数为偶数的数字的个数

  eg:input :  nums = [12,345,2,6,7896]

    output : 2(其中12和7896是偶数位数)

 

 1 def findNumbers(nums):
 2     #方法一
 3     num = [str(i) for i in nums]
 4     count = 0
 5     i = 0
 6     while i < len(nums):
 7         if len(num[i]) % 2 == 0:
 8             count += 1
 9         i += 1
10     return count
11 
12     #方法二
13     #num = map(str,nums)
14     #count = 0
15     #for i in num:
16     #if len(i) % 2 == 0:
17         #count += 1
18             #return count   
19 if __name__ == "__main__":
20     nums = [12,345,2,6,7896]
21     print(findNumbers(nums))

输出结果:2

posted on 2020-01-03 09:55  dangdangA  阅读(108)  评论(0编辑  收藏  举报