![](/img/trans.png)
[英]Python nested for loop does not execute 2nd for loop on 2nd iteration of 1st loop
[英]Loop gets stuck on 1st iteration
在嘗試創建網絡爬蟲時,我正在嘗試定義此函數:
#a and b are lists.
def union(a, b):
i = 0
while i <= len(b):
if b[i] in a :
a = a.append(b[i])
i = i + 1
return a
然后測試它,我輸入:
a = [1,2,3]
b = [2,4,6,9]
union(a,b)
它不斷給我錯誤
TypeError: argument of type 'NoneType' is not iterable
我已經在另一個線程中看到了針對此問題的解決方案,方法是將我的條件編輯為if a and b[i] not in a:
,但是在測試時,這只能解決它,直到我將 b 的 1 個元素附加到 a,然后停止工作.
請參閱下面的更新和注釋代碼
#a and b are lists.
def union(a, b):
i = 0
while i <= len(b):
if b[i] in a :
# the problem lies here. append returns None
# a = a.append(b[i])
# this should work
a.append(b[i])
i = i + 1
return a
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.