Skip to content Skip to sidebar Skip to footer

How Can I Increment Array With Loop?

I've got two lists: listOne = ['33.325556', '59.8149016457', '51.1289412359'] listTwo = ['2.5929778', '1.57945488999', '8.57262235411'] I use len to tell me how many items are in

Solution 1:

You mean to do:

while iterations < itemsInListOne:
    listOne[iterations] = float(listOne[iterations]) + 1

Note that you needed to convert it to a float before adding to it.

Also note that this could be done more easily with a list comprehension:

listOne = [float(x) + 1 for x in listOne]

Solution 2:

since you can't increment a list (it's technically not an array), I assume you want to increment each list element. If this is the case you might want something like this:

iterations = 0

while iterations < itemsInListOne:
    listOne[iterations] += 1
    print iterations
    iterations += 1

I hope I understood you right, if not so, please consider rewriting your question, to make it clearer. Thanks

Solution 3:

# You could do it in one line using list comprehension in Pythondefloop_and_increment(data):                                                   
    return [ str(float(_) + 1) for _ in data]                                   

print loop_and_increment(listOne)                                               
print loop_and_increment(listTwo)

Post a Comment for "How Can I Increment Array With Loop?"