list的append与extend
2022/1/7 6:03:35
本文主要是介绍list的append与extend,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1.针对一维
append: 列表添加一个元素(直接嵌套一个list)
l1 = [2, 3, 4] l2 = [7, 8, 9] l1.append(l2) l1 [2, 3, 4, [7, 8, 9]]
extend:两个列表合并:
l1 = [2, 3, 4] l2 = [7, 8, 9] l1.extend(l2) l1 [2, 3, 4, 7, 8, 9]
2.针对二维的情况
appen:
l1 = [[2, 3, 4],[2, 3, 4]] l2 = [[7, 8, 9],[7, 8, 9]] l1.append(l2) >>> l1 [[2, 3, 4], [2, 3, 4], [[7, 8, 9], [7, 8, 9]]]
extend:
l1 = [[2, 3, 4],[2, 3, 4]] l2 = [[7, 8, 9],[7, 8, 9]] l1.extend(l2) >>> l1 [[2, 3, 4], [2, 3, 4], [7, 8, 9], [7, 8, 9]]
3.二维形成一个新的list
extend:
import numpy as np l=[] l1 = [[2, 3, 4],[2, 3, 4]] l2 = [[7, 8, 9],[7, 8, 9]] l.extend(l1) l.extend(l2) >>> l [[2, 3, 4], [2, 3, 4], [7, 8, 9], [7, 8, 9]] >>> np.concatenate(l,axis=0) array([2, 3, 4, 2, 3, 4, 7, 8, 9, 7, 8, 9])
append:
import numpy as np l=[] l1 = [[2, 3, 4],[2, 3, 4]] l2 = [[7, 8, 9],[7, 8, 9]] l.append(l1) l.append(l2) >>> l [[[2, 3, 4], [2, 3, 4]], [[7, 8, 9], [7, 8, 9]]] >>> np.concatenate(l,axis=0) array([[2, 3, 4], [2, 3, 4], [7, 8, 9], [7, 8, 9]])
这篇关于list的append与extend的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-23线下车企门店如何实现线上线下融合?
- 2024-12-23鸿蒙Next ArkTS编程规范总结
- 2024-12-23物流团队冬至高效运转,哪款办公软件可助力风险评估?
- 2024-12-23优化库存,提升效率:医药企业如何借助看板软件实现仓库智能化
- 2024-12-23项目管理零负担!轻量化看板工具如何助力团队协作
- 2024-12-23电商活动复盘,为何是团队成长的核心环节?
- 2024-12-23鸿蒙Next ArkTS高性能编程实战
- 2024-12-23数据驱动:电商复盘从基础到进阶!
- 2024-12-23从数据到客户:跨境电商如何通过销售跟踪工具提升营销精准度?
- 2024-12-23汽车4S店运营效率提升的核心工具