我在变量x和y中有两个字符串,其中x的长度大于y的长度。如何将较小的字符串(y)映射到大字符串(x)的大小?
例如:
x = "appleappleapple" # (length 15)
y = "abc" # (length 3) This should be mapped to "abcabcabcabcabc"
while len(y) < len(x):
y+=y
while len(y) > len(x):
y = y[:-1]
print x
print y
appleappleapple
abcabcabcabcabc
是否有一种非常pythonic /有效的方法来进行此操作?
答案 0 :(得分:4)
x = "appleappleapple" # (length 15)
y = "abc" # (length 3) This should be mapped to "abcabcabcabcabc"
z = (y*(len(x)/len(y)+1))[:len(x)]