为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

森林海 2024-01-16 15:49:29
简而言之,我想打印三个数组,每个数组之间都有字符串。例如,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) 
查看完整描述

2 回答

?
守着一只汪

TA贡献1872条经验 获得超3个赞

将“<html>”放入 zip 将迭代该字符串中的所有 6 个字符,然后完成。尝试:

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



查看完整回答
反对 回复 2024-01-16
?
隔江千里

TA贡献1906条经验 获得超10个赞

zip正在将其工作截断为传递给它的最短迭代;它的参数之一的长度仅为 6


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

[(0, 0, 0), (1, 1, 1), (2, 2, 2)]


查看完整回答
反对 回复 2024-01-16
  • 2 回答
  • 0 关注
  • 36 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信