函数的参数位置如果采用 *参数名,则可以传递的参数都保存在一个元组当中。
>>> def intersect(*args): #求交集
... res=[]
... for x in args[0]:
... for other in args[1:]: #args【1:】是出去其他的各个项目
... if x not in other:break
... else:
... res.append(x)
... return res
...
>>> def union(*args): #求并集
... res=[]
... for seq in args:
... for x in seq:
... if not x in res:res.append(x)
... return res
...
>>> s1,s2,s3="PAM","SCAM","SLAM"
>>> intersect(s1,s2,s3) #传递3个参数。
['A', 'M']
>>> union(s1,s3,s2) #传递3个参数。
['P', 'A', 'M', 'S', 'L', 'C']
阅读(538) | 评论(0) | 转发(0) |