簡而言之,我想打印三個數(shù)組,每個數(shù)組之間都有字符串。例如,print(item + string + description + string + price). 我有 3 個數(shù)組,但此代碼僅打印前六行。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 上還沒有得到解答,我發(fā)現(xiàn)的一切都不是實用的而是理論的。這是我的完整代碼: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貢獻(xiàn)1872條經(jīng)驗 獲得超4個贊
將“<html>”放入 zip 將迭代該字符串中的所有 6 個字符,然后完成。嘗試:
res = "\n".join("{} {} {} {} {} {} {}".format(html, x, html, z, html, w, html) for x, z, w in zip(itemList, priceList, descripList))

隔江千里
TA貢獻(xiàn)1906條經(jīng)驗 獲得超10個贊
zip正在將其工作截斷為傳遞給它的最短迭代;它的參數(shù)之一的長度僅為 6
>> [x for x in zip(range(3), range(4), range(5))]
[(0, 0, 0), (1, 1, 1), (2, 2, 2)]
添加回答
舉報
0/150
提交
取消