解决方案:
>>> s1 = 'abcd'
>>> s2 = 'efghi'
>>> if len(s1) > len(s2):
... (s1, remainder) = (s1[0:len(s2)], s1[len(s2):])
... elif len(s1) < len(s2):
... (s2, remainder) = (s2[0:len(s1)], s2[len(s1):])
... else:
... remainder = ""
Now that we have two strings with the same length plus an optional remainder we could try using an index to loop over the two strings and interleave the characters.
>>> # s1 and s2 now start out as two equal length strings
>>> chars = []
>>> for i in range(len(s1)):
... chars.append(s1[i])
... chars.append(s2[i])
>>> result = "".join(chars) + remainder
>>> result
'aebfcgdhi'
链接:
http://simeonfranklin.com/blog/2012/nov/2/ask-expert-interlacing-strings/
外国人写的解决方法,时间有限,没能一一仔细阅读,先标记着,有空了再研究。