2 回答
TA贡献1811条经验 获得超6个赞
因为整数的位数只是其表示方式(以 10 为基数)的副产品,所以您必须将其转换为字符串。
x = 100
y = 1298411291836199301
x = str(x)
target_len = len(str(y))
while len(x) < target_len:
x += x
# Cut off the last loop if it goes over the
# desired length, and turn it back into an int
x = int(x[:target_len])
# >>> x
# 1001001001001001001
TA贡献1877条经验 获得超6个赞
您可以使用
x = 100
y = 1298411291836199301
n = len(str(y))
x = str(x)
m = len(x)
multiplier = n // m + 1
x = ''.join( # join an iterable of strings into a single string
(x for _ in range(multiplier)) # generator expression that returns x multiple times
)[:n] # truncate the final string to the exact desired length
x = int(x)
print(x)
print(y)
输出
1001001001001001001
1298411291836199301
添加回答
举报