如何在Python中循环遍历数组并并排打印每个索引?

简而言之,我想打印三个数组,每个数组之间都有字符串。例如,


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

我有 3 个数组,但此代码仅打印前六行。


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)

我很惊讶像这样简单的事情在 Stack Overflow 上还没有得到解答,我发现的一切都不是实用的而是理论的。


这是我的完整代码:


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) 


森林海
浏览 43回答 2
2回答

守着一只汪

将“<html>”放入 zip 将迭代该字符串中的所有 6 个字符,然后完成。尝试:res&nbsp;=&nbsp;"\n".join("{}&nbsp;{}&nbsp;{}&nbsp;{}&nbsp;{}&nbsp;{}&nbsp;{}".format(html,&nbsp;x,&nbsp;html,&nbsp;z,&nbsp;html,&nbsp;w,&nbsp;html)&nbsp;for&nbsp;x,&nbsp;z,&nbsp;w&nbsp;in&nbsp;zip(itemList,&nbsp;priceList,&nbsp;descripList))

隔江千里

zip正在将其工作截断为传递给它的最短迭代;它的参数之一的长度仅为 6>> [x for x in zip(range(3), range(4), range(5))][(0, 0, 0), (1, 1, 1), (2, 2, 2)]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python