我目前正在为房地产数据建立一个网络刮刀。我在Python中工作,遇到了一个我似乎无法修复的错误。
for i in range(len(s)):
if '$' in s[i]:
price.append(s[i])
elif 'bath' in s[i]:
left = s[i].partition(",")[0]
right = s[i].partition(",")[2]
bed_bath.append(left)
sqft_lot.append(right)
elif 'fort collins' in s[i].lower():
address0 = s[i-1]+' '+s[i]
address.append(address0)
elif s[i].lower() == 'advertisement':
del s[i]
else:
continue存在的价值:
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Firefox()
browser.get(realtor.format(format))
p = browser.find_element(By.XPATH, "//ul[@class='jsx-343105667 property-list list-unstyle']")
content = p.text
s = re.split('\n',content)这基本上应该是迭代数组的,并将它们添加到单独的数组价格bed_bath、sqrft_lot、DataFrame中使用的地址中。我知道这是正确的索引,我已经连续地打印了i在范围(len(S))中使用的每一行: print si,它可以工作,但是当我试图实现逻辑时,它只是中断了。
获取错误:
if '$' in s[i]:
**IndexError: list index out of range**对于为什么会发生这种情况,我们将不胜感激。
发布于 2022-02-19 17:39:43
正如@quamrana所提到的,最有可能的问题是您执行了del s[i],所以s变得更短了,因此一些索引将不再存在于s中。我有两个可能的解决办法。修正1:
for i in range(len(s)):
if i >= len(s): # check if index is still in bounds
break
if '$' in s[i]:
price.append(s[i])
elif 'bath' in s[i]:
left = s[i].partition(",")[0]
right = s[i].partition(",")[2]
bed_bath.append(left)
sqft_lot.append(right)
elif 'fort collins' in s[i].lower():
address0 = s[i-1]+' '+s[i]
address.append(address0)
elif s[i].lower() == 'advertisement':
del s[i]
else:
continue修正2:
indexes_to_remove = []
for i in range(len(s)):
if '$' in s[i]:
price.append(s[i])
elif 'bath' in s[i]:
left = s[i].partition(",")[0]
right = s[i].partition(",")[2]
bed_bath.append(left)
sqft_lot.append(right)
elif 'fort collins' in s[i].lower():
address0 = s[i-1]+' '+s[i]
address.append(address0)
elif s[i].lower() == 'advertisement':
indexes_to_remove.append(i)
else:
continue
for index in indexes_to_remove[::-1]: # if you iterate through it backward, you won't have that problem.
del s[i]发布于 2022-02-19 17:42:39
从s中创建一个新列表,并使用筛选和处理的数据填充它。
output_list = []
def process_data(value):
# your code for processing data
...
for i in range(len(s)):
if s[i] == some_condition(i):
output_list.append(process_value(s[i])发布于 2022-02-19 17:49:03
你要在for循环中删除。这里的示例也会引发一个错误,并且可能更容易理解:
s = [i for i in range(5)]
for i in range(len(s)):
print(f"{i=} with {s=}")
del s[i]输出:
IndexError: list assignment index out of range
i=0 with s=[0, 1, 2, 3, 4]
i=1 with s=[1, 2, 3, 4]
i=2 with s=[1, 3, 4]
i=3 with s=[1, 3]https://stackoverflow.com/questions/71187437
复制相似问题