简体   繁体   English

如何在整数中每 200 打印一个 X?

[英]How to print an X per 200 in an integer?

My code:我的代码:

x = [232,  456, 1024 , 245]
x_ = []
for i in range (0, len(x)):
    while x[i] >= 200:
        x_.insert(x[i], 'X')
        x[i] = x[i] - 200
print(x_)

Output: ['X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X']输出: ['X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X']

Wanted Output: ['X', 'XX', 'XXXXX','X']想要的输出: ['X', 'XX', 'XXXXX','X']

I essentially want to insert an X for every 200 in the element.我基本上想为元素中的每 200 个插入一个 X。 So if an element in the list is let's say 456, the new list will have 'XX' in x_[2].因此,如果列表中的元素是 456,则新列表将在 x_[2] 中包含“XX”。

您可以将'X'乘以除以 200 的元素:

x_ = ['X' * (y // 200) for y in x]

You are currently inserting new 'X' in the list instead of appending your current entry.您当前正在列表中插入新的'X'而不是附加您当前的条目。 If you want to keep your structure try:如果你想保持你的结构尝试:

x_ = []
for i in range (0, len(x)):
    xs = 'X'
    x[i] -= 200
    while x[i] >= 200:
        xs += 'X'
        x[i] -= 200
    x_.append(xs)
print(x_)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM