小白教程
所有教程
关于
Search
172.70.126.177
172.70.126.177
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Python3 att list extend”的源代码
本页内容
上一节:
Python3_att_list_count
下一节:
Python3_att_list_index
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:list.extend(seq)}}[[Category:python3 list|7]] = Python3 List extend()方法 = [[python3 list|Python3 列表]] == 描述 == extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。 == 语法 == extend()方法语法: <sample title="" desc="" lang="python" hererun="1"> list.extend(seq) </sample> == 参数 == * seq -- 元素列表,可以是列表、元组、集合、字典,若为字典,则仅会将键(key)作为元素依次添加至原列表的末尾。 == 返回值 == 该方法没有返回值,但会在已存在的列表中添加新的列表内容。 以下实例展示了 extend()函数的使用方法: <sample title="" desc="" lang="python" hererun="1"> #!/usr/bin/python3 list1 = ['Google', 'xiaoBai', 'Taobao'] list2=list(range(5)) # 创建 0-4 的列表 list1.extend(list2) # 扩展列表 print ("扩展后的列表:", list1) </sample> 以上实例输出结果如下: <sample title="" desc="" lang="python" hererun="1"> 扩展后的列表: ['Google', 'xiaoBai', 'Taobao', 0, 1, 2, 3, 4] </sample> 不同数据类型: <sample title="" desc="" lang="python" hererun="1"> #!/usr/bin/python3 # 语言列表 language = ['French', 'English', 'German'] # 元组 language_tuple = ('Spanish', 'Portuguese') # 集合 language_set = {'Chinese', 'Japanese'} # 添加元组元素到列表末尾 language.extend(language_tuple) print('新列表: ', language) # 添加集合元素到列表末尾 language.extend(language_set) print('新列表: ', language) </sample> <sample title="" desc="" lang="python" hererun="1"> 新列表: ['French', 'English', 'German', 'Spanish', 'Portuguese'] 新列表: ['French', 'English', 'German', 'Spanish', 'Portuguese', 'Chinese', 'Japanese'] </sample> [[python3 list|Python3 列表]]
返回至“
Python3 att list extend
”。
上一节:
Python3_att_list_count
下一节:
Python3_att_list_index