最近做一个项目,需要在列表(list)中添加字符串信息,现说一下extend方法的使用。
描述:extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。
语法:list.extend(seq)
参数:seq -- 元素列表。
返回值:该方法没有返回值,但会在已存在的列表中添加新的列表内容。

extend()函数的具体使用方法:
# 描述:extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。
# 语法:list.extend(seq)
# 参数:seq -- 元素列表。
# 返回值:该方法没有返回值,但会在已存在的列表中添加新的列表内容。
list=['123','张三','san.zhang']
list.extend([12,'李四'])
print('添加的数据是:',list)

注:extend()函数使用注意事项:
当添加数据时,请添加“[]”,例如:[2019,'li.si']、[2019]、[lisi"]

错误的写法:
list=['123','张三','san.zhang']
#错误的写法
list.extend('2019')
print('添加的数据是:',list)

网友评论