There are several values on the list I would like to ask you how to compare these values and if the previous value is less than the later value, proceed as it is, and if the previous value is greater than the later value.
For example, if you have ['14', '2', '3', '3', '5', '17'] in list a, you want to change this to ['14', '14', '15', '15', '17', '17'] in this way.
python
Is there a number in the list you gave me as an example? Does it have text? First of all, in the questions and comments, we created a code that returns a string list by expressing it as a string.
If the array contains only numbers, you can erase all of the type transition functions, such as int() and str() codes.
And you said, "If the previous value is less than the later value, proceed as it is," but I didn't understand clearly whether it was less than the value after adding 12 or less than the value of the original array. First of all, we've arranged to return the result as expected (I want to change this element to ['14', '14', '15', '15', '17']).
If the result is less than the value after adding 12, it becomes the logic.
# Array to be explored
target = ['14', '2', '3', '3', '5', '17']
print ("===before conversion ===")
print(target)
# ['14', '2', '3', '3', '5', '17']
# Assume that the number in the target array is greater than or equal to 0
# Assume that there is at least one value in the list
# Variable that temporarily stores the largest of the past values
tempMax = int(target[0])
for index, value in enumerate(target):
intValue = int(value)
if intValue < tempMax:
newValue = intValue + 12
target[index] = str(newValue)
tempMax = newValue
print ("===after conversion ===")
print(target)
# ['14', '14', '15', '15', '17', '17']
© 2024 OneMinuteCode. All rights reserved.