我有一个这样的字符串:
var q = from c in campaingsIds
join e in emailClicked on c.CampaingId equals e.CampaingId
select new Response()
{
CampaingId = c.CampaingId,
RecipientEmailAddress = e.RecipientEmailAddress,
SenderEmailAddress = c.SenderEmailAddress
};
我想要以下输出:
sentence = 'This is a nice day'
在这种情况下,我在output = ['This is', 'a nice', 'day']
= 3或更多的空格处分割了字符串,这就是为什么像上面显示的那样分割字符串的原因。
如何为任何n
有效地做到这一点?
答案 0 :(得分:5)
您可以尝试使用Python的正则表达式拆分:
sentence = 'This is a nice day'
output = re.split(r'\s{3,}', sentence)
print(output)
['This is', 'a nice day']
要为实际变量n
处理此问题,我们可以尝试:
n = 3
pattern = r'\s{' + str(n) + ',}'
output = re.split(pattern, sentence)
print(output)
['This is', 'a nice day']
答案 1 :(得分:2)
您可以使用基本的.split()函数:
sentence = 'This is a nice day'
n = 3
sentence.split(' '*n)
>>> ['This is', 'a nice day']
答案 2 :(得分:2)
您还可以按n
个空格进行拆分,剥离结果并删除空元素(如果有几个这样的长空格会产生它们):
sentence = 'This is a nice day'
n = 3
parts = [part.strip() for part in sentence.split(' ' * n) if part.strip()]
答案 3 :(得分:0)
getAllColumnGroups