【问题标题】:Python List Population with Loop带有循环的 Python 列表填充
【发布时间】:2017-06-19 18:26:51
【问题描述】:

我无法让下面的 county 列表填充我循环的结果。当我打印出每次迭代的结果以及列表中项目的索引时,我看到每次都得到一个 0 的索引,这表明数据在每次循环后没有保留在列表中。因此,当我在循环完成后尝试对 county 循环进行索引时,当然其中根本没有数据,所以我得到了“列表索引超出范围错误”。

我研究了我不断收到的“列表索引超出范围”错误,我知道我收到它是因为 county 列表是空的,但为什么它是空的?

构成target_divs 列表中一项的 HTML 源代码如下所示:

<div class="school-type-list-text">
<div class="table_cell_county"><a href='/alabama/autauga-county'>Autauga County</a></div>
<div class="change_div"></div>
<div class="table_cell_other">7<span> Schools</span></div>
<div class="table_cell_other">1,587<span> Students</span></div>
<div class="table_cell_other">8%<span> Minority</span></div>
<div class="break"></div>

这是我的脚本:

import urllib2
from bs4 import BeautifulSoup
import pandas
import csv

page1 = 'https://www.privateschoolreview.com/alabama'
alabama = urllib2.urlopen(page1)
soup = BeautifulSoup(alabama, "lxml")
target_divs = soup.find_all("div", class_= "school-type-list-text")

for i in target_divs:
    county = i.find_all("div", class_= "table_cell_county")
    for i in county:
        print i.text
        print county.index(i) 

print county
print county[0]

@Software2 建议更改循环光标后更新,但我仍然收到相同的错误:

import urllib2
from bs4 import BeautifulSoup
import pandas
import csv

page1 = 'https://www.privateschoolreview.com/alabama'

alabama = urllib2.urlopen(page1)

soup = BeautifulSoup(alabama, "lxml")

target_divs = soup.find_all("div", class_= "school-type-list-text")

for div in target_divs:
    counties = div.find_all("div", class_= "table_cell_county")
    for county in counties:
        print county.text
        print counties.index(county) 

print counties

【问题讨论】:

  • 您有两个引用 ifor 循环
  • OP 已经粘贴了代码的输出。请不要编辑。

标签: python list


【解决方案1】:

我可能错了,你可以试试这个。看来您在嵌套循环中使用相同的 i

for i in target_divs:
    county = i.find_all("div", class_= "table_cell_county")
    for j in county:
        print j.text
        print county.index(j) 

【讨论】:

    【解决方案2】:

    您在嵌套循环中使用相同的变量i 作为两个不同的东西。所以第一个被覆盖了。更改第二个变量名。

    理想情况下,像i 这样的变量名不是很容易描述,而且很容易犯这样的错误。尝试类似:

    for div in target_divs:
        counties = div.find_all("div", class_= "table_cell_county")
        for county in counties:
            print county.text
            print counties.index(county) 
    

    【讨论】:

    • 进行了更改,但 counties 仍未填充。有什么额外的想法吗?我已经在上面的帖子中更新了我的代码,所以你可以确保我听从了你的建议。
    【解决方案3】:

    我假设您想要counties 中的县列表。在我看来,问题在于div.find_all() 的返回值,它最多返回一个县的数组。要填充县,请尝试以下方法:

    counties = []
    for div in target_divs:
        county = div.find_all('div', class_= 'table_cell_county')
        for c in county:
            counties.append(c.text.encode('utf-8'))
    
    print counties    # Returns: ['Autauga County', 'Baldwin County', 'Barbour County', 'Bibb County', 'Blount County', 'Bullock County', 'Butler County', 'Calhoun County', 'Chambers County', 'Chilton County', 'Choctaw County', 'Clarke County', 'Clay County', 'Coffee County', 'Colbert County', 'Conecuh County', 'Covington County', 'Crenshaw County', 'Cullman County', 'Dale County', 'Dallas County', 'Dekalb County', 'Elmore County', 'Escambia County', 'Etowah County', 'Greene County', 'Hale County', 'Henry County', 'Houston County', 'Jackson County', 'Jefferson County', 'Lauderdale County', 'Lee County', 'Limestone County', 'Lowndes County', 'Macon County', 'Madison County', 'Marengo County', 'Marion County', 'Marshall County', 'Mobile County', 'Monroe County', 'Montgomery County', 'Morgan County', 'Perry County', 'Pickens County', 'Pike County', 'Randolph County', 'Russell County', 'Saint Clair County', 'Shelby County', 'Sumter County', 'Talladega County', 'Tallapoosa County', 'Tuscaloosa County', 'Walker County', 'Wilcox County', 'Winston County']
    print counties[0] # Returns: 'Autauga County'
    

    【讨论】:

    • @SFarkas 没问题!此外,如果您可以投票或将其标记为答案,它也会对其他人有所帮助:)
    猜你喜欢
    • 1970-01-01
    • 2023-01-14
    • 2013-07-18
    • 1970-01-01
    • 2016-02-23
    • 2021-07-06
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    相关资源
    最近更新 更多