亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在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條經驗 獲得超4個贊

將“<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 關注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號