Is there a way to split a string by every nth separator in Python?

前端 未结 6 1983
时光取名叫无心
时光取名叫无心 2020-12-05 23:17

For example, if I had the following string:

\"this-is-a-string\"

Could I split it by every 2nd \"-\" rather than every \"-\" so that it returns two values (\

6条回答
  •  孤街浪徒
    2020-12-06 00:04

    l = 'this-is-a-string'.split()
    nl = []
    ss = ""
    c = 0
    for s in l:
       c += 1
       if c%2 == 0:
           ss = s
       else:
           ss = "%s-%s"%(ss,s)
           nl.insert(ss)
    
    print nl
    

提交回复
热议问题