反向连接2个字符串
Concatenation of 2 strings in reverse
我有 2 个字符串:
str1 = 12345
str2 = abcde
因此我需要具备以下条件:
5a4b3c2d1e
这怎么可能?
您可以使用 map 和 zip 来完成,但您还必须将 int 转换为字符串。
str1 = 12345
str2 = "abcde"
foo = ''.join(map(''.join, zip(str(str1)[::-1], str2)))
print(foo)
5a4b3c2d1e
[::-1] 反转作为 int
的字符串
zip() 将两个字符串分成对(元组列表)
[('5', 'a'), ('4', 'b'), ('3', 'c'), ('2', 'd'), ('1', 'e')]
first .join 使其进入列表
['5a', '4b', '3c', '2d', '1e']
second .join 将列表转换为字符串
5a4b3c2d1e
我有 2 个字符串:
str1 = 12345
str2 = abcde
因此我需要具备以下条件:
5a4b3c2d1e
这怎么可能?
您可以使用 map 和 zip 来完成,但您还必须将 int 转换为字符串。
str1 = 12345
str2 = "abcde"
foo = ''.join(map(''.join, zip(str(str1)[::-1], str2)))
print(foo)
5a4b3c2d1e
[::-1] 反转作为 int
的字符串zip() 将两个字符串分成对(元组列表)
[('5', 'a'), ('4', 'b'), ('3', 'c'), ('2', 'd'), ('1', 'e')]
first .join 使其进入列表
['5a', '4b', '3c', '2d', '1e']
second .join 将列表转换为字符串
5a4b3c2d1e