python list 重复元素不会覆盖

python list 插入重复元素不会覆盖,见以下测试:

bash 复制代码
Python 3.10.12 (main) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [1, 2, 3]
>>> print(a)
[1, 2, 3]
>>> a.append(4)
>>> print(a)
[1, 2, 3, 4]
>>> a.append(2)
>>> print(a)
[1, 2, 3, 4, 2]
>>> a.append('6')
>>> print(a)
[1, 2, 3, 4, 2, '6']
>>> a.append('6')
>>> print(a)
[1, 2, 3, 4, 2, '6', '6']

如果需要重复元素不被添加,需自行判断元素是否存在,见以下demo:

bash 复制代码
if item not in object_list:
	object_list.append(item)

python 列表基本操作

Python 表达式 结果 描述
len(1, 2, 3) 3 长度
1, 2, 3 + 4, 5, 6 1, 2, 3, 4, 5, 6 组合
'Hi!' * 4 'Hi!', 'Hi!', 'Hi!', 'Hi!' 重复
3 in 1, 2, 3 True 元素是否存在于列表中
for x in 1, 2, 3: print x, 1 2 3 迭代
相关推荐
cup1113 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi0015 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵16 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf17 小时前
Agent 流程编排
后端·python·agent
copyer_xyf18 小时前
Agent RAG
后端·python·agent
copyer_xyf18 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf18 小时前
Agent 记忆管理
后端·python·agent
星云穿梭1 天前
用Python写一个带图形界面的学生管理系统——完整教程
python
金銀銅鐵1 天前
用 Pygame 实现 15 puzzle
python·数学·游戏