简体   繁体   中英

how to loop through arrays and print each index side by side in python?

Simply put, I want to print three arrays with strings in between each. For example,

print(item + string + description + string + price). 

I have the 3 arrays but this code only prints the first six lines.

res = "\n".join("{} {} {} {} {} {} {}".format(html, x, html, z, html, w, html) for x, y, z, v, w in zip(itemList, html, priceList, html, descripList)) 
print(res)

I'm surprised something simple like this is not already answered on Stack Overflow, everything I find is not practical but rather theoretical.

Here is my full code:

itemList=[]
for tag in soup.find_all('a', class_=['menuItem-name']):
    if tag not in itemList:
        itemList.append(tag.text)

descripList=[]
for t in soup.find_all('span', class_=['u-text-secondary']):
    if t not in descripList:
        descripList.append(t.text)

priceList=[]
for g in soup.find_all('p', class_=['menuItem-displayPrice']):
    if g not in priceList:
        priceList.append(g.text)

html="<html>"
res = "\n".join("{} {} {} {} {} {} {}".format(html, x, html, z, html, w, html) for x, y, z, v, w in zip(itemList, html, priceList, html, descripList))
print(res) 

putting "<html>" into zip will iterate through all 6 chars in that string and then finish. Try:

res = "\n".join("{} {} {} {} {} {} {}".format(html, x, html, z, html, w, html) for x, z, w in zip(itemList, priceList, descripList))

zip is truncating its work at the shortest iterable passed to it; one of its arguments only has a length of 6

>> [x for x in zip(range(3), range(4), range(5))]
[(0, 0, 0), (1, 1, 1), (2, 2, 2)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM