根据另一个列表中的项目数创建多个新列表



我创建了一个包含5个项目的列表,然后我创建了newlist,它是使用文本和列表中的项目值访问网页的路径

我希望能够循环浏览newlist中的项目,并根据newlist中的项目数量创建需要创建的列表

因此,在这个例子中,最终会创建10个新列表:

buyers1
buyers2
buyers3
buyers4
buyers5
prices1
prices2
prices3
prices4
prices5

这可以循环浏览每个网页并打印值,但我不能引用外部的列表(无论如何,每次都会被覆盖(

from lxml import html
import requests
list = ['1','2','3','4','5']
newlist = []
for l in list:
newlist.append('http://econpy.pythonanywhere.com/ex/00'+str(l)+'.html')
for n in newlist:
page = requests.get(n)
tree = html.fromstring(page.content)
buyers = tree.xpath('//div[@title="buyer-name"]/text()')
prices = tree.xpath('//span[@class="item-price"]/text()')   
for b in buyers :print(b)
for p in prices :print(p)

但是我不能通过在之后添加这个来引用for循环之外的这些新列表(即使它们每次都被覆盖(

for b in buyers :print(b)
for p in prices :print(p)

所以这是我创建新列表的尝试,但没有成功:

list = ['1','2','3','4','5']
value = 1
newlist = []
for l in list:
newlist.append('http://econpy.pythonanywhere.com/ex/00'+str(l)+'.html')
for n in newlist:
page = requests.get(n)
tree = html.fromstring(page.content)
'buyers' + str(value) = tree.xpath('//div[@title="buyer-name"]/text()')
'prices' + str(value) = tree.xpath('//span[@class="item-price"]/text()')   
value = value + 1

所以最终我可以在之后参考该列表

for b in buyers1:
print(b)

如果我没有错,你想根据买家和价格创建新的列表吗?这可能有效-

newlist = ['http://econpy.pythonanywhere.com/ex/00'+str(i)+'.html' for i in range(1, 6)]
buyers, prices = [], []
for n in newlist:
page = requests.get(n)
tree = html.fromstring(page.content)
buyers.append(tree.xpath('//div[@title="buyer-name"]/text()')
prices.append(tree.xpath('//span[@class="item-price"]/text()')   

现在,您可以在for循环之外访问buyersprices。如果我误解了你的问题,请告诉我

假设我了解问题所在,我建议拥有一个列表列表或一个值为list类型的字典,因为不知道预期的列表数量,可迭代对象就是你想要的

使用列表的解决方案:

from lxml import html
import requests
list = ['1','2','3','4','5']
newlist = []
listOfBuyers = []
listOfPrices = []
for l in list:
newlist.append('http://econpy.pythonanywhere.com/ex/00'+str(l)+'.html')
for n in newlist:
page = requests.get(n)
tree = html.fromstring(page.content)
buyers = tree.xpath('//div[@title="buyer-name"]/text()')
prices = tree.xpath('//span[@class="item-price"]/text()')   
for b in buyers :print(b)
for p in prices :print(p)
listOfBuyers.append(buyers)
listOfPrices.append(prices)

使用字典的解决方案:

from lxml import html
import requests
list = ['1','2','3','4','5']
newlist = []
dictOfBuyers = {}
dictOfPrices = {}
for l in list:
newlist.append('http://econpy.pythonanywhere.com/ex/00'+str(l)+'.html')
i = 1
for n in newlist:
page = requests.get(n)
tree = html.fromstring(page.content)
buyers = tree.xpath('//div[@title="buyer-name"]/text()')
prices = tree.xpath('//span[@class="item-price"]/text()')   
for b in buyers :print(b)
for p in prices :print(p)
keyBuyers = "buyer" + str(i)
keyPrices = "price" + str(i)
dictOfBuyers[keyBuyers] = buyers
dictOfPrices[keyPrices] = prices
i += 1

对于从字典中读取数据,重要的是要知道字典中的值是而不是索引的,因此我们需要从其关键字中读取每个值,如下所示:

for key, val in dictOfBuyers.items() :
listBuyers = val
listPrices = dictOfPrices["price" + key[-1]]
print(listBuyers,"is corresponds to the prices",listPrices)

最新更新