Python 循环在第一次迭代时被卡住

Python 循环在第一次迭代时被卡住,python,list,if-statement,for-loop,web-crawler,Python,List,If Statement,For Loop,Web Crawler,在尝试创建web爬虫时,我尝试定义以下函数: #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 它总是给我错误 TypeError: argument of type 'NoneType' is not iterabl

在尝试创建web爬虫时,我尝试定义以下函数:

#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
它总是给我错误

TypeError: argument of type 'NoneType' is not iterable

我在另一个线程中看到了这个问题的解决方案,如果a和b[I]不在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
#a和b是列表。
def接头(a、b):
i=0

而我下次请正确格式化你的代码。只需单击“编辑”,然后单击“格式为代码”。使用,而不是列表。集合具有内置的
联合
,您可以利用它。不需要自己滚动。这不是python
while
循环的工作方式。请阅读基本的Python教程。请参阅
append
返回
None
因此
a=a.append(b[i])
有效地用
None
覆盖
a
的值。只需执行
a.append(b[i])
#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