网站首页 > 技术文章 正文
Python添加列表
4 分钟阅读
在 Python 操作列表有各种方法。例如 – 简单地将一个列表的元素附加到 for 循环中另一个列表的尾部,或使用 +/* 运算符、列表推导、extend() 和 itertools.chain() 方法。
Python 添加列表 – 6 种连接/连接列表的方法
用于添加两个列表的 for 循环
这是添加两个列表的最直接的编程技术。
- 使用 for 循环遍历第二个列表
- 保留在第一个列表中附加元素
- 第一个列表将动态扩展
# Python Add lists example
# Sample code to add two lists using for loop
# Test input lists
in_list1 = [21, 14, 35, 16, 55]
in_list2 = [32, 25, 71, 24, 56]
# Using for loop to add lists
for i in in_list2 :
in_list1.append(i)
# Displaying final list
print ("\nResult: **********\nConcatenated list using for loop: "
+ str(in_list1))
加号 (+) 运算符以合并两个列表
许多语言使用 + 运算符来追加/合并字符串。Python也支持它,甚至支持列表。
这是一个使用“+”运算符的简单合并操作。您可以通过将一个列表添加到另一个列表的背面来轻松合并列表。
# Sample code to merge two lists using + operator
# Test input lists
in_list1 = [21, 14, 35, 16, 55]
in_list2 = [32, 25, 71, 24, 56]
in_list3 = in_list1 + in_list2
# Displaying final list
print ("\nResult: **********\nPython merge list using + operator: "
+ str(in_list3))
用于联接列表的 Mul (*) 运算符
这是一种连接两个或多个列表的全新方法,可从 Python 3.6 获得。您可以应用它来连接多个列表并一个统一列表。
# Python join two lists
# Sample code to join lists using * operator
# Test input lists
in_list1 = [21, 14, 35, 16, 55]
in_list2 = [32, 25, 71, 24, 56]
in_list3 = [*in_list1, *in_list2]
print ("\nResult: **********\nPython join list using * operator: "
+ str(in_list3))
列表推导以连接列表
列表推导允许对输入列表进行任何操作(串联),并且可以毫不费力地生成一个新操作。此方法像在“for”循环中一样处理列表,即逐个元素。
in_list1 = [21, 14, 35, 16, 55]
in_list2 = [32, 25, 71, 24, 56]
in_list3 = [n for m in [in_list1, in_list2] for n in m]
print ("\nResult: **********\nPython concatenate list using list comprehension: "
+ str(in_list3))
内置列表扩展() 方法
此函数是 Python 列表类的一部分,也可用于添加或合并两个列表。它对原始列表进行就地扩展。
in_list1 = [21, 14, 35, 16, 55]
in_list2 = [32, 25, 71, 24, 56]
in_list1.extend(in_list2)
print ("\nResult: **********\nPython Add lists using list.extend(): "
+ str(in_list1))
itertools.chain() 来组合列表
Python itertools chain() 函数接受多个可迭代对象并生成单个列表。输出是将所有输入列表串联为一个。
import itertools
# Test input lists
in_list1 = [21, 14, 35, 16, 55]
in_list2 = [32, 25, 71, 24, 56]
in_list3 = list(itertools.chain(in_list1, in_list2))
print ("\nResult: **********\nPython join lists using itertools.chain(): "
+ str(in_list3))
- 上一篇: python入门到脱坑经典案例—清空列表
- 下一篇: python中列表类型
猜你喜欢
- 2025-08-03 Python列表方法append和extend的区别
- 2025-08-03 Python列表集合操作介绍?
- 2025-08-03 python数据类型之列表、字典、元组、集合及操作
- 2025-08-03 Python学不会来打我(11)列表list详解:用法、场景与类型转换
- 2025-08-03 Python骚操作从列表推导和生成器表达式开始
- 2025-08-03 Python中的列表详解及示例
- 2025-08-03 Python自动化办公应用学习笔记20—列表排序、列表推导式
- 2025-08-03 python入门012:复制列表
- 2025-08-03 Python列表元素求偶:简洁实现与解析
- 2025-08-03 python入门经典案例—list列表翻转列表
- 08-05python决策树用于分类和回归问题实际应用案例
- 08-05用Python实现机器学习算法之k-决策树算法并做注释说明
- 08-05Python机器学习之决策树分类详解,保姆级教学!
- 08-05用Python进行机器学习(5)-决策树
- 08-05决策树算法原理与Python实现
- 08-05python学习笔记 1.常见的数据类型
- 08-05从进阶语法到实战应用:Python中级修炼指南
- 08-05Python 面试问题:运算符
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)