带有逗号但带有某些条件的Python连接字符串列表-代码折射
假设我有这个列表带有逗号但带有某些条件的Python连接字符串列表-代码折射,python,string,list,python-2.7,list-comprehension,Python,String,List,Python 2.7,List Comprehension,假设我有这个列表l: l = ['a', 'b', 'c', 'd', 'e'] 我知道我可以使用将它们连接起来。连接时使用逗号分隔为: s = ', '.join(l) >>> a, b, c, d, e 但我想在加入时尊重这些条件,很少有情况是: 如果len(l)==1,则输出应为a 如果len(l)==2,则输出应为a和b 如果len(l)>2且len(l)=5: 如果len(l)==5,则输出应为a、b、c、d和1个其他 如果len(l)>5,则输出应为a、b、
l
:
l = ['a', 'b', 'c', 'd', 'e']
我知道我可以使用将它们连接起来。连接时使用逗号分隔为:
s = ', '.join(l)
>>> a, b, c, d, e
但我想在加入时尊重这些条件,很少有情况是:
- 如果
len(l)==1
,则输出应为a
- 如果
len(l)==2
,则输出应为a和b
- 如果
len(l)>2且len(l)<5
,则输出应为a、b、c和d
- 如果
len(l)>=5
:
- 如果
len(l)==5
,则输出应为a、b、c、d和1个其他
- 如果
len(l)>5
,则输出应为a、b、c、d和+(剩余字符串数)其他
我所尝试的(工作):
这个代码可以改进和折射吗 这正是您想要的:
def frmt_lst(l, limit=5):
if len(l) == 1:
return l[0]
if len(l) < limit:
return '{} and {}'.format(', '.join(l[:-1]), l[-1])
if len(l) == limit:
return '{} and 1 other'.format(', '.join(l[:-1]))
if len(l) > limit:
return '{} and {} others'.format(', '.join(l[:limit-1]), len(l[limit-1:]))
print frmt_lst(['a'])
print frmt_lst(['a', 'b'])
print frmt_lst(['a', 'b', 'c'])
print frmt_lst(['a', 'b', 'c', 'd'])
print frmt_lst(['a', 'b', 'c', 'd', 'e'])
print frmt_lst(['a', 'b', 'c', 'd', 'e', 'f'])
>>>
a
a and b
a, b and c
a, b, c and d
a, b, c, d and 1 other
a, b, c, d and 2 others
def frmt(l,极限=5):
如果len(l)==1:
返回l[0]
如果len(l)<极限:
返回'{}和{}'。格式(','.join(l[:-1]),l[-1])
如果len(l)=极限:
返回“{}和1 other.”格式(“,”.join(l[:-1]))
如果长度(l)>极限:
返回'{}和{}其他'.格式(','.join(l[:limit-1]),len(l[limit-1:]))
打印首字母(['a'])
打印首字母(['a',b'])
打印首字母(['a','b','c'])
打印首字母(['a','b','c','d'])
打印首字母(['a','b','c','d','e'])
打印首字母(['a','b','c','d','e','f'])
>>>
A.
a和b
a、 b和c
a、 b、c和d
a、 b、c、d和1个其他
a、 b、c、d和其他2个
我注意到你只有四个特例。因此,我将代码更改为更简单。只是一个想法(可能过于复杂)
def display(l, t = 5):
length = len(l)
if length <= 2: print " and ".join(l)
elif length < threshold: print ", ".join(l[:-1]) + " and " + l[-1]
elif length == threshold: print ", ".join(l[:-1]) + " and 1 other"
else: print ", ".join(l[:t-1]) + " and +{} others".format(length - (t - 1))
试试@AshwiniChaudhary我应该删除这个并发布到那里吗?或者任何有特权将其迁移到那里的人?我已经将其标记为版主关注。我非常确定,几个月前,对于几乎相同的问题,有一个问题需要一个Pythonic解决方案,并且有几个好的答案。我的谷歌技能让我无法找回它。任何人?elif
最好避免编译器检查不必要的条件,也不想修复阈值。谢谢:)elif
和多次len
检查可以避免。+1很好的解决方案,先生,我不知道为什么我总是先采用复杂的方法:/length=len(l)
不需要,因为len(l)
更快。它不是每次都计算它,它已经存储为一个变量。@InbarRoselen(l)
速度很快,但如果你查看字节码,你会看到大量调用LOAD\u GLOBAL
len
。因此,只调用一次就可以避免那些不必要的调用。我认为如果elif是一个“and”.join(l)
调用,您可以合并前两个调用。@AshwiniChaudhary谢谢yaar:)
def frmt_lst(l, limit=5):
if len(l) == 1:
return l[0]
if len(l) < limit:
return '{} and {}'.format(', '.join(l[:-1]), l[-1])
if len(l) == limit:
return '{} and 1 other'.format(', '.join(l[:-1]))
if len(l) > limit:
return '{} and {} others'.format(', '.join(l[:limit-1]), len(l[limit-1:]))
print frmt_lst(['a'])
print frmt_lst(['a', 'b'])
print frmt_lst(['a', 'b', 'c'])
print frmt_lst(['a', 'b', 'c', 'd'])
print frmt_lst(['a', 'b', 'c', 'd', 'e'])
print frmt_lst(['a', 'b', 'c', 'd', 'e', 'f'])
>>>
a
a and b
a, b and c
a, b, c and d
a, b, c, d and 1 other
a, b, c, d and 2 others
def display(l, t = 5):
length = len(l)
if length <= 2: print " and ".join(l)
elif length < threshold: print ", ".join(l[:-1]) + " and " + l[-1]
elif length == threshold: print ", ".join(l[:-1]) + " and 1 other"
else: print ", ".join(l[:t-1]) + " and +{} others".format(length - (t - 1))
a
a and b
a, b and c
a, b, c and d
a, b, c, d and 1 other
a, b, c, d and +2 others
def join_func(inlist, threshold=5):
llen = len(inlist)
if llen==1:
return inlist[0]
suffix_len = llen - threshold
prefix = ','.join(inlist[:min(llen, threshold)-1*(suffix_len<=0)])
return ' and '.join([prefix, inlist[-1] if suffix_len<=0 else
'{} other{}'.format(suffix_len, 's'*(suffix_len>1))])
In [69]: for l in xrange(1, 8):
print join_func(list(string.ascii_letters[:l]))
....:
a
a and b
a,b and c
a,b,c and d
a,b,c,d and e
a,b,c,d,e and 1 other
a,b,c,d,e and 2 others
def frm_list(l, limit=5):
length = len(l)
if length == 1:
return l[0]
if length > limit:
l_joins = ', '.join(l[:limit-1])
others = length - limit
plural = others > 1 and 's' or ''
return u'{} and {} other{}'.format(l_joins, others, plural)
l_joins = ', '.join(l[:-1])
return u'{} and {}'.format(l_joins, l[-1])