list对象的三种扩充方式
2018-02-24 本文已影响0人
3f31023436c0
第一种,使用.extend函数,添加新列表到原有列表中。注意添加的形式是列表。
List=[]
for X in A:
if not 函数(X):
List .extend([函数(X)])
print(List)
第二种,使用.append函数,挨个元素添加。注意添加的形式是元素而不是列表(不加[])。
List=[]
for X in A:
if not 函数(X):
List .append(函数(X))
print(List)
第三种,使用+=,也就是自加。挨个列表添加。
List=[]
for X in A:
if not 函数(X):
List+=[函数(X)]
print(List)
这三种添加列表的方法,得到的结果是一样的。