列表的extend函数
功能
- 将其他列表或元组中的
元素导入当前列表中
用法
list.extend(iterable)
参数
iterable代表列表或元组 , 该函数无返回值
注意事项
- 传入的必须是
iterable - 直接传入字符串的话会被拆分成很多个单个字符
- 不可传入整形或者布尔类型之类的(不是
iterable就不行) - 传入
字典的话只会保留key的值
代码
| # coding:utf-8 | |
| manhua = [] | |
| history = [] | |
| code = [] | |
| new_manhua = ('a', 'b', 'c') | |
| new_history = ('中国历史', '日本历史', '韩国历史') | |
| new_code = ('python', 'django', 'flask') | |
| manhua.extend(new_manhua) | |
| history.extend(new_history) | |
| code.extend(new_code) | |
| print(manhua, history, code) | |
| history.extend(manhua) | |
| del manhua | |
| print(history) | |
| test = [] | |
| # test.extend('abcd') | |
| test.extend({'name': 'dewei', 'age': 33}) | |
| # test.extend(True) | |
| print(test) | |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 ZkeqのCoding日志!
评论






