0

im incrementing a counter that leads to an index of a list

code:

delist = []
i = 1
idx = [0, ..., 10, ..., 100, ..., 1000, ...]

while( i < len(idx)):

    if i in range(1, 10):
        delist.append("ladybug_cube_00000" + str(idx[i]) + "_0.jpg")

    elif i in range(10, 100):
        delist.append("ladybug_cube_0000" + str(idx[i]) + "_0.jpg")

    elif i in range(100, 1000):
        delist.append("ladybug_cube_000" + str(idx[i]) + "_0.jpg")

    elif i in range(1000, 10000):
        delist.append("ladybug_cube_00" + str(idx[i]) + "_0.jpg")

    elif i in range(10000, 100000):
        delist.append("ladybug_cube_0" + str(idx[i]) + "_0.jpg")

    elif i in range(100000, 1000000):
        delist.append("ladybug_cube_" + str(idx[i]) + "_0.jpg")

    #this deletes that index from the list
    del idx[i]

    #this goes an step back so it repeats the loop with the new index's variable.
    i = i-1
i= i+1

The issue is that it should store every string into a new index in the list "delist" and every element should have exacly 6 digits after "cube_" and before "_0.jpg", but it is storing an extra 0 before "str(idx[i])".

example output:

delist=["ladybug_cube_000001_0.jpg", "ladybug_cube_000010_0.jpg", "ladybug_cube_000100_0.jpg", "ladybug_cube_001000_0.jpg", "ladybug_cube_010000_0.jpg", "ladybug_cube_100000_0.jpg"]

What I'm getting:

delist=["ladybug_cube_000001_0.jpg", "ladybug_cube_0000010_0.jpg", "ladybug_cube_00000100_0.jpg", "ladybug_cube_000001000_0.jpg", "ladybug_cube_0000010000_0.jpg", "ladybug_cube_00000100000_0.jpg"] 

like it won't pass from "range(1, 10)".

Thriskel
  • 351
  • 3
  • 13
  • 2
    You can pad your string with zeros to the desired length: https://stackoverflow.com/a/339013/8881141 Use simply `"ladybug_" + str(i).zfill(7) + "_0.jpg"`. – Mr. T Mar 06 '18 at 18:01
  • Something in the indentation might have gone wrong when you posted. Should i=i+1 be inside the loop? If yes, then i doesn't really change, and is always in range(1,10). As MrT has already noted, thee are simpler ways to pad with zeroes. Also, there probably are simpler was to iterate over a list. – Sebastian Mar 06 '18 at 18:05
  • @Sebastian indentation is ok, I tried swaping conditions order and it always takes range(1,10). i always increments, it only repeats itself if it's necessary. – Thriskel Mar 06 '18 at 18:13
  • 1
    Should it be `if idx[i] in range…`? – Amaro Vita Mar 06 '18 at 18:16
  • @AmaroVita Thank you, I thought of that and posted the answer, anyway you are right. – Thriskel Mar 06 '18 at 18:20

3 Answers3

1

The problem is these line:

i = i - 1
i = i + 1

You're essentially always looping through i = 1, so the first condition i in range(1, 10) will always fire and give you 5 zeroes padding.

As the other users have mentioned you should just pad the zeroes in the string instead of the multiple conditions.

delist.append("ladybug_cube_{:06}_0.jpg".format(idx[i]))

Better yet, there's no point in having both i and idx since the index is irrelevant (it's the equivalent of the values). You can just have idx = list(range(100000)) and loop through idx instead of doing all these index managing, like so:

idx = list(range(100000))
for i in idx:
    delist.append("ladybug_cube_{:06}_0.jpg".format(i))

You might even further simplify your code to this list comprehension:

delist = ["ladybug_cube_{:06}_0.jpg".format(i) for i in range(100000)]

Output:

['ladybug_cube_000000_0.jpg',
 'ladybug_cube_000001_0.jpg',
 ...
 'ladybug_cube_000010_0.jpg',
 'ladybug_cube_000011_0.jpg',
 'ladybug_cube_000012_0.jpg',
 ...
 'ladybug_cube_000100_0.jpg',
 'ladybug_cube_000101_0.jpg',
 'ladybug_cube_000102_0.jpg',
 ...]
r.ook
  • 13,466
  • 2
  • 22
  • 39
0

Changed the conditions from:

if/elif i in range(x,y):

to

if/elif int(idx[i]) in range(x,y):

since it was using i counter instead of the real value idx[i] that it required to properly work.

Thriskel
  • 351
  • 3
  • 13
0

Your script confuses me, but if I am not mistaken, this replicates your script:

idx = [0, 1, 2, 14, 121, 123100]
delist = ["ladybug_cube_" + str(i).zfill(6) + "_0.jpg" for i in idx]

#output
['ladybug_cube_000000_0.jpg', 'ladybug_cube_000001_0.jpg', 'ladybug_cube_000002_0.jpg', 'ladybug_cube_000014_0.jpg', 'ladybug_cube_000121_0.jpg', 'ladybug_cube_123100_0.jpg']
Mr. T
  • 11,960
  • 10
  • 32
  • 54