Chinaunix首页 | 论坛 | 博客
  • 博客访问: 61319
  • 博文数量: 9
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 190
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-19 16:36
文章分类

全部博文(9)

文章存档

2008年(9)

我的朋友
最近访客

分类: LINUX

2008-03-10 16:50:18

函数的参数位置如果采用 *参数名,则可以传递的参数都保存在一个元组当中。
>>> 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) |
给主人留下些什么吧!~~