【问题标题】:List index out of range while creating jagged array创建锯齿状数组时列表索引超出范围
【发布时间】:2018-09-09 21:07:48
【问题描述】:

所有,我正在尝试在 Python 3.x 中创建一个锯齿状列表。具体来说,我从使用 Selenium 的网页列表中提取了一些元素。我的锯齿状列表(“矩阵”)的每一行代表这些网页之一的内容。这些行中的每一行都应该有与从其各自网页中提取的元素一样多的列——这个数字会因页面而异。

例如

webpage1 has 3 elements: a,b,c
webpage2 has 6 elements: d,e,f,g,h,i
webpage3 has 4 elements: j,k,l,m
...

看起来像:

[[a,b,c],
[d,e,f,g,h,i],
[j,k,l,m],...]

到目前为止,这是我的代码:

from selenium import webdriver

chromePath = "/Users/me/Documents/2018/chromedriver"
browser = webdriver.Chrome(chromePath)

url = 'https://us.testcompany.com/eng-us/women/handbags/_/N-r4xtxc/to-1'
browser.get(url)

hrefLinkArray = []

hrefElements = browser.find_elements_by_class_name("product-item")

for eachOne in hrefElements:
    hrefLinkArray.append(eachOne.get_attribute('href'))

pics = [[]]

for y in range(0, len(hrefLinkArray)): # or type in "range(0, 1)" to debug
    browser.get(hrefLinkArray[y])
    productViews = browser.find_elements_by_xpath("// *[ @ id = 'lightSlider'] / li")
    b = -1
    for a in productViews:
        b = b + 1
        # print(y) for debugging
        # print(b) for debugging
        pics[y][b] = a.get_attribute('src') # <------------ ERROR!
        # pics[y][b].append(a.get_attribute('src') GIVES SAME ERROR AS ABOVE
    del productViews[:]

browser.quit()

每当我运行它时,我都会在a in productViews 循环的第一次迭代中收到错误:

line 64, in <module>
    pics[y][b] = a.get_attribute('src')
IndexError: list assignment index out of range

据我所知,整数引用是正确的(请参阅我在for a in productViews 循环中的调试行),因此pics[0][0] 是引用锯齿状列表的正确方法。说到这里,我有一种感觉pics[0][0]还不存在?或者也许只有pics[0] 可以?我看过关于这个错误的类似帖子,但我理解的唯一解决方案似乎是使用.append(),即便如此,在一维列表上使用它。正如您在我的代码中看到的那样,我将.append() 用于hrefLinkArray 成功,而它在第64/65 行显示为不成功。我很困惑为什么会这样。

请告诉我:

  1. 为什么我的行 .append()[][]=... 会抛出这个错误。

  2. 如果有更有效的方法来实现我的目标,我愿意学习!

更新:使用@User4343502 的回答,结合@StephenRauch 的输入,错误得到解决,我现在得到了预期大小的锯齿状列表!我修改后的代码是:

listOfLists = []

for y in range(0, len(hrefLinkArray)):
    browser.get(hrefLinkArray[y])

    productViews = browser.find_elements_by_xpath("// *[ @ id = 'lightSlider'] / li")
    otherList = []
    for other in productViews:
        otherList.append(other.get_attribute('src'))
        # print(otherList)
    listOfLists.append(otherList)
    del otherList[:]
    del productViews[:]

print(listOfLists)

请注意,此代码会打印一个完全为空的索引的锯齿状列表,例如[[][],[][][][],[],[][][],[][],[][][][][]...],但这是一个单独的问题 - 我相信与我的productViews 对象以及它如何通过xpath 检索有关...不过,重要的是我的原始问题得到了回答。谢谢!

【问题讨论】:

  • 这是很多代码,如果您将其剥离到仅相关部分(关于列表的部分)会有所帮助
  • @StephenRauch,您附加的“解决方案”没有回答为什么我在第 65 行的代码(在错误行下方注释掉)不起作用。我会尝试它建议的解决方法的要点,但即使它有效(我会报告这个),我不明白为什么我以前的尝试不起作用......这可能是一个解决方案,但不是答案。这有意义吗?
  • 答案解释了错误,即您无法处理不存在的列表元素。
  • @StephenRauch。所以 pics[0][0] 还不存在,即使我声明了 pics[[]] ?如果是这种情况,那么pics[[]] 是什么?如果是这种情况,那么使用 pics.append(someOtherArrayContainingThePageLinks) 之类的东西是您附加链接的建议吗?

标签: python python-3.x list web-scraping jagged-arrays


【解决方案1】:

list.append 会将一个元素添加到列表中。无论元素是什么,这都有效。

a = [1, 2, 3]
b = [float, {}]
c = [[[None]]]

## We will append to this empty list
list_of_lists = []

for x in (a, b, c):
    list_of_lists.append(x)

## Prints: [[1, 2, 3], [<type 'float'>, {}], [[[None]]]]
print(list_of_lists)

Try it Online!

【讨论】:

    猜你喜欢
    • 2014-12-27
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 2020-08-09
    • 2020-04-26
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多