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

时间:2018-08-01 10:01:46

标签: python list

我创建了一个包含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)

2 个答案:

答案 0 :(得分:1)

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

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。让我知道我是否错误地理解了您的问题

答案 1 :(得分:0)

假设我了解问题所在,我建议您使用列表列表或具有类型为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)