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']
Wanted Output: ['X', 'XX', 'XXXXX','X']
I essentially want to insert an X for every 200 in the element. So if an element in the list is let's say 456, the new list will have 'XX' in x_[2].
您可以将'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. 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_)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.