0

Hej Everyone,

The idea of the script is to grab image links from a catalogue page of my company's website and change them to image links with a higher resolution and filter for image format, where the variable to filter for is found in the link itself, in this case the capital P. Afterwards a csv is generated with the links.

The transformation, filtering and writing to csv works fine, but my problem is that I don't want all the 80 products, I only want 8 to be in the list nl.

The links list contains elements like this one https://rndr.mywebsite.com/media/catalog/product/seo-cache/x386/19/95/19-95-101P/How-Hard-You-Hit-Butcher-Billy-Premium-Poster.jpg

NOTE: variables ratio and creatives (inputnumber-1) are defined by commandline input. Just assume that the input was ratio = P and creatives = 9-1.

NOTE2: For quicker testing, the links list has a limit of 15 elements by now.

nl= []
string1= "https://rndr.mywebsite.com/media/catalog/product/cache/x800/"
string2= ".jpg"

while len(nl) <= creatives:                        
    for index in range(len(links)):
        if "P" in "".join(links[index].split("/", 12)[10]) and "P" in ratio:
            print("YEAH", len(nl))
            nl.extend([string1 + "/".join(links[index].split("/", 11)[8:11]) + string2])
        else:
            print ("Ups", len(nl))
print (nl)

The actual output is

('YEAH', 0)
('YEAH', 1)
('YEAH', 2)
('YEAH', 3)
('Ups', 4)
('YEAH', 4)
('YEAH', 5)
('Ups', 6)
('YEAH', 6)
('YEAH', 7)
('YEAH', 8)
('YEAH', 9)
('YEAH', 10)
('YEAH', 11)
('YEAH', 12)
[https://rndr.mywebsite.com/media/catalog/product/cache/x800/19/95/19-95-101P.jpg, transformed-link2,...,transformed-link12]

As you can see the filtering and transforming works fine, but it should stop after having 9 links in the list nl.

2
  • not necessarily, because you're doing that in a loop. Commented Jul 3, 2017 at 14:17
  • You're extending nl with another list of, I don't know what size inside another loop. You're probably overshooting the limit before the next check can be done. Commented Jul 3, 2017 at 14:18

3 Answers 3

2

As mentioned by Coldspeed, in the inner loop you're adding a whole batch of items to nl, thus overshooting the limit. To fix it, you could get rid of the while loop and do this instead:

for index in range(len(links)):
    if "P" in "".join(links[index].split("/", 12)[10]) and "P" in ratio:
        print("YEAH", len(nl))
        nl.append(string1 + "/".join(links[index].split("/", 11)[8:11]) + string2)
        if len(nl) > creatives: 
            break
    else:
        print ("Ups", len(nl))
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, didn't see you'd already posted. Linked your answer to mine. :)
Okay, your way is smarter than my initial idea. Thanks for your answer and fixing my code :)
1

Adding a couple of print statements like this can help you figure out exactly what is going on:

while len(nl) <= creatives:     
    print('outer loop')                   
    for index in range(len(links)):
        print('inner loop')
        ...

You've got a nested loop here. What happens is, inside the inner loop, the condition for the outer loop is not checked, until the inner loop has finished iterating. What you'd need to do is put an explicit break inside the inner loop.

Look at this answer for a solution. :)

2 Comments

Ah, thank you for the explanation what I constructed there. I already fiddled around with break but was not aware of the problem that it checks the condition. Will have a look at the link you provided :)
@NilsOle It links to Błotosmętek's solution which you've already accepted. My work here is done.
1

You're doing a for loop inside the while loop. The while loop will only check its condition upon finishing the first for loop, by which point you've already looped over every element in links.

E.g.

i = 0
while i < 10:
    for z in range(20):
        i = z
        print(i)

will print all the way to 19, because the precondition for the while loop will only be checked when the inner for loop finishes.

1 Comment

Thank you very much for explaining what my mistake was and giving a practical explanation what happens in my code :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.