我正在编写一个函数,它接受
*args
参数,并返回一个字典,其中键从1到9,值为能够被1到9整除而没有余数的数字数量:my_dict = {}
def myFunc(*args):
for item in args:
if (item % 2 == 0):
my_dict[2] = number of times arguments were divisible by 2
if (item % 3 == 0):
my_dict[3] = number of times arguments were divisible by 3
...
myFunc(1,5,6,10,5,8)
我尝试过这个:
my_dict = {}
def myFunc(*args):
x=0
for item in args:
if (item % 2) == 0:
x+=1
my_dict[2] = x
myFunc(1, 2, 3, 6, 8,10)
print(my_dict)
#{2: 4}
这个方案适用于一个数字,但我不确定如何优雅地填充整个字典,以便在传入1,2,3,4,4,5,10,16,20
时看起来像这样:
my_dict = {1:9, 2:6, 3:1, 4:4, 5:3, 6:0, 7:0, 8:1, 9:0}
如何解决这个问题?