0

BeautifulSoupの結果から作成したリストからアイテムを削除しようとしています。私のforループは、if条件とelif条件の基準に一致するいくつかの項目(すべてではありません)を削除しているようです。助けてくれませんか?

from BeautifulSoup import BeautifulSoup
import urllib
import re

# url constructor for search
pass
pass
pass

# read the site into a string
site = urllib.urlopen("http://losangeles.craigslist.org/moa/")
html = site.read()
site.close()
soup = BeautifulSoup(html)



#Price
prices = soup.findAll("span", {"class":"itempp"})
prices = [str(j).strip('<span class="itempp"> $</span>') for j in prices]
print prices
print len(prices)

for k in prices:
    if k == '':
        prices.remove(k)
    elif int(k) <= 50:
        prices.remove(k)

print prices
print len(prices)

戻り値:

['230', '120', '1', '333', '180', '380', '120', '1', '230', '25', '300', '500', '480', '199', '600', '180', '260', '200', '35', '250', '10', '80', '200', '20', '20', '', '20', '15', '10', '', '35', '225', '9', '', '79', '280', '30', '10', '10', '80', '1', '20', '', '15', '20', '200', '550', '20', '700', '350', '25', '30', '99', '', '', '40', '', '200', '25', '20', '180', '', '120', '50', '700', '120', '380', '320', '130', '325', '175', '180', '120', '260', '5', '450', '650', '400', '700', '50', '600', '250', '699', '245', '649', '450', '615', '450', '665', '5', '5', '50', '1100', '30', '300', '700', '15', '12', '12', '10']
100
['230', '120', '333', '180', '380', '120', '230', '300', '500', '480', '199', '600', '180', '260', '200', '250', '80', '200', '35', '225', '79', '280', '10', '10', '80', '20', '20', '200', '550', '20', '700', '350', '99', '', '', '200', '20', '180', '', '120', '700', '120', '380', '320', '130', '325', '175', '180', '120', '260', '450', '650', '400', '700', '600', '250', '699', '245', '649', '450', '615', '450', '665', '5', '1100', '30', '300', '700', '15', '12', '10']
71

**最初の1つは削除されていますが、その後は10が残っていることに注意してください。また、いくつかの''も削除されています。

みんなありがとう

4

1 に答える 1

1

pricesループ中にいくつかのアイテムを削除する場合は、リストのコピーを反復処理する必要があります。試す:

for k in prices[:]:
    if k == '':
        prices.remove(k)
    elif int(k) <= 50:
        prices.remove(k)

またはリスト内包表記を使用する:

prices[:] = [p for p in prices if p and int(p) > 50]
于 2012-12-02T08:26:14.427 に答える