加入中容易交替的分隔符

Cas*_*sie 3 python string delimiter implode

我有一个像这样的元组列表(字符串是填充...我的实际代码具有未知的值):

list = [
  ('one', 'two', 'one'),
  ('one', 'two', 'one', 'two', 'one'),
  ('one', 'two', 'one', 'two', 'one', 'two', 'one'...)
]
Run Code Online (Sandbox Code Playgroud)

我想在<strong> </strong>标签中包装每个其他字符串(在此示例中为"两个"字符串).我无法做到令人沮丧,'<strong>'.join(list)因为其他人都没有/.这是我能想到的唯一方法,但是使用旗帜困扰着我...而且我似乎无法在谷歌机器上找到关于这个问题的任何其他内容.

def addStrongs(tuple):
  flag = False
  return_string = ""
  for string in tuple:
    if flag :
      return_string += "<strong>"
    return_string += string
    if flag :
      return_string += "</strong>"
    flag = not flag
  return return_string

formatted_list = map(addStrongs, list)
Run Code Online (Sandbox Code Playgroud)

我很抱歉,如果这是错误的,我仍然是python的新手.有一个更好的方法吗?我觉得这在其他方面也很有用,就像添加左/右引号一样.

unb*_*eli 5

>>> tuple = ('one', 'two', 'one', 'two', 'one')
>>> ['<strong>%s</strong>' % tuple[i] if i%2 else tuple[i] for i in range(len(tuple))]
['one', '<strong>two</strong>', 'one', '<strong>two</strong>', 'one']
Run Code Online (Sandbox Code Playgroud)


jhi*_*erd 5

from itertools import cycle
xs = ('one', 'two', 'one', 'two', 'one')
print [t % x for x, t in zip(xs, cycle(['<strong>%s</strong>', '%s']))]
Run Code Online (Sandbox Code Playgroud)

使用cycleyou 可以应用于比“其他”更复杂的模式。