2016-12-31 73 views

回答

6

如果这两个词是相同的长度,你可以使用zip

''.join(x for p in zip(*"hello world".split(" ")) for x in p) 
# 'hweolrllod' 

''.join(x for p in zip("hello", "world") for x in p) 
# 'hweolrllod' 

如果它们的长度相同的没有了,你要保持较长的版本,使用zip_longest

from itertools import zip_longest  
''.join(x for p in zip_longest(*"he world".split(" "), fillvalue='') for x in p) 
# 'hweorld' 

''.join(x for p in zip_longest("he", "world", fillvalue='') for x in p) 
# 'hweorld' 
+2

不错的答案,我upvoted它,但它会更容易理解,如果你使用更多的描述性变量,如'''.join(字符为zip中的对(*“hello world”.split(“”))为字符成对)'。 –

+0

有什么方法可以将每个字母移动一定的数量,例如 – Tailor

+0

编辑*有没有办法将每个字母移动一定的量,例如你好,世界。我如何向右移动电子邮件,以及移动多少电子邮件。请以最简单的形式! – Tailor

相关问题