|
发表于 2017-6-12 17:43:04
|
显示全部楼层
酱紫?
- def likes(ls):
- fn=(lambda x:"no one",
- lambda x:x[0],
- lambda x:' and '.join(x))
- if len(ls)<3 :
- ret = fn[len(ls)](ls)
- else:
- a,b,*c = ls
- ret = "%s, %s and %s" % (a,b,(c[0]if len(c)==1 else '%s others'%len(c)))
- return "%s like%s this" % (ret,('','s')[len(ls)<2])
- ls=[[],
- ["Peter"],
- ["Jacob", "Alex"],
- ["Max", "John", "Mark"],
- ["Alex", "Jacob", "Mark", "Max"]]
- for l in ls:
- print(likes(l))
复制代码
- no one likes this
- Peter likes this
- Jacob and Alex like this
- Max, John and Mark like this
- Alex, Jacob and 2 others like this
复制代码 |
|