gpt4 book ai didi

Python - deq 函数打印 8 次 - 防止只打印一次?

转载 作者:行者123 更新时间:2023-12-01 09:06:44 25 4
gpt4 key购买 nike

所以我玩了一下 deq,几乎快要完成了,但有人担心由于 deq 的长度,它打印了 8 次,而我只想打印一次。

我所做的是:

old_list = []
deq = deque(old_list, maxlen=8)
url = 'https://www.supremecommunity.com/restocks/eu/'

while True:
try:
new_list = []

bs4 = soup(requests.get(url).text, "html.parser")

for item in bs4.findAll('div', {'class': 'restock-item'}):
if item.find('div', {'class': 'user-detail'}):
name = item.find('h5', {'class': 'handle restock-name'}).string
color = item.find('h6', {'class': 'restock-colorway'}).string

new_list.append(name + color)

for newitem in new_list:
if newitem not in deq:
print(name)
print(color)
deq.append(newitem)

else:
print('Sleeping 5 sec')
time.sleep(5)
except:
continue

基本上它会检查网站并打印出名称和颜色,然后将其添加到 deq 列表中。然而,由于 maxlen=8,我的输出打印出 8 次相同的名称和颜色,我的问题是:

如何才能让它只打印一次?

最佳答案

您始终打印与上面的 for 循环中最后定义的相同的变量 namecolor

      name = item.find('h5', {'class': 'handle restock-name'}).string
color = item.find('h6', {'class': 'restock-colorway'}).string

当您在第二个 for 循环中打印 print(name)print(color) 时,它始终引用最后一个值名称颜色具有。

要解决这个问题,您应该在打印语句中引用变量newitem

编辑:

这里您只是连接两个字符串。

new_list.append(name + color)

我建议您将其设为列表的列表。

new_list.append([name,color])

然后您可以使用 print(newitem[0])print(newitem[1]) 打印不同的名称和颜色。

关于Python - deq 函数打印 8 次 - 防止只打印一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51983062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com