我有
k= (('answer ', ' Answer the call for a channel(answer )'), ('att_xfer ', ' Attended Transfer(att_xfer )'), ('bind_digit_action ', ' Bind a key sequence or regex to an action.(bind_digit_action )'))
我想剥去所有额外的空间。我怎么能这样做
答案 0 :(得分:4)
如果您的意思是“额外空格”,则表示每个字符串开头和结尾的空格:
k = tuple(tuple(b.strip() for b in a) for a in k)
如果您想删除字符串中的其他“额外空格”(例如(answer )
=> (answer)
),则必须定义更多规则。
答案 1 :(得分:2)
如果你想删除所有空格:
tuple(tuple("".join(i.split()) for i in a) for a in k)
出:
(('answer', 'Answerthecallforachannel(answer)'),
('att_xfer', 'AttendedTransfer(att_xfer)'),
('bind_digit_action',
'Bindakeysequenceorregextoanaction.(bind_digit_action)'))
或者如果您不需要元组:
from itertools import chain
["".join(i.split()) for i in chain.from_iterable(k)]
出:
['answer',
'Answerthecallforachannel(answer)',
'att_xfer',
'AttendedTransfer(att_xfer)',
'bind_digit_action',
'Bindakeysequenceorregextoanaction.(bind_digit_action)']
答案 2 :(得分:1)
另一种方式是:
tuple(map(lambda x:tuple(map(lambda y:y.strip(),x)),k))