Working on my FOR loop - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Working on my FOR loop (/thread-14571.html) |
Working on my FOR loop - wilsonrivas - Dec-06-2018 Hello friends! Actually I'm a little new on phyton and I been working on a scrape file; in a part on code is like: isver = paged_soup.find_all('span', {'class': 'a-size-mini a-color-state a-text-bold'}, href=True) isver = [ifv.text for ifv in isver]If it all is right, this build a 10 items vector and isver must have a value like: [Verified Purchase, Verified Purchase, Verified Purchase, Verified Purchase, Verified Purchase, Verified Purchase, Verified Purchase, Verified Purchase, Verified Purchase, Verified Purchase] But the problem come when the page have less than 10 Verified Purchase items, these make a short vectors. For example: if it get 2 items found the vector is like: [Verified Purchase, Verified Purchase] But I always looking to build a 10 item vector like: [No Verified Purchase, Verified Purchase, No Verified Purchase, No Verified Purchase, Verified Purchase, No Verified Purchase, No Verified Purchase, No Verified Purchase, No Verified Purchase, No Verified Purchase] (since I don't know many Verified Purchase will found, the order can be variable) Them I try change the code at something like this: rcounter=0 for r in isver: rcounter = rcounter + 1 if (r == '<span class="a-size-mini a-color-state a-text-bold" data-hook="avp-badge">Verified Purchase</span>'): if (rcounter <= 10): isver = [isver.append(r.text)] else: if (rcounter <=10): r = 'No Verified Purchase' isver = [isver.append(r)]This not build any vector at all and everytime I print r the value is : <span class="a-size-mini a-color-state a-text-bold" data-hook="avp-badge">Verified Purchase</span> But it doesn't match on If contidion and do directly here. else: if (rcounter <=10): r = 'No Verified Purchase' isver = [isver.append(r)]And also r value is replace to all For loop. What can I do in that case?, I'll appreciate some idea; Thanks in advance. Wilson Rivas. RE: Working on my FOR loop - heiner55 - May-24-2019 #!/usr/bin/python3 import random #create array with random length isver = [ "Verfified Purchase" ] * random.randint(0,10) print(len(isver), isver) print() #now we want always ten elements isver += ["Verified Purchase New"] * (10 - len(isver)) print(len(isver), isver) |