stack=s+stack#TypeError: can only concatenate str (not “list“) to str

PYTHON的神奇操作

复制代码
stack=['1','2']
s="ddd"
# stack=s+stack#TypeError: can only concatenate str (not "list") to str
stack+=s
print(stack)#['1', '2', 'd', 'd', 'd']
复制代码
stack=['1','2']
s="ddd"
stack=s+stack  # 这里会报错,因为不能直接将字符串和列表相加
stack+=s  # 这行不会报错,会将字符串 s 追加到列表 stack 中

在 Python 中,不能直接将字符串和列表进行相加操作,这会引发 TypeError 错误。但使用 += 操作符时,如果操作对象是列表,它会尝试将右侧的可迭代对象(如字符串)的元素逐个追加到列表中。

相关推荐
言乐613 小时前
Python实现建造微服务商城后台
开发语言·python·算法·微服务·架构
fenglllle13 小时前
chromadb emmbedding 向量检索
人工智能·python·embedding
cui_ruicheng13 小时前
Python从入门到实战(六):非序列容器
开发语言·python
飞猪~14 小时前
Langchain python版本 LLM 重要函数invoke,ainvoke,stream,astream,batch,abatch
python·langchain·batch
沙蒿同学14 小时前
当古诗词遇上 AI:从 38 万句诗词中取一个好名字
python·算法·架构
xxie12379415 小时前
Python装饰器与语法糖
开发语言·python
CClaris15 小时前
大模型量化从0到1(五):GPTQ 原理详解 + 从零量化一个真实大模型
人工智能·python·算法·机器学习
Railshiqian15 小时前
UserPickerActivity 内部逻辑分析
开发语言·python
一tiao咸鱼15 小时前
前端转 agent # 02 - FastAPI 框架入门与原理
前端·python
Yolo566Q16 小时前
Noah-MP陆面过程模型建模方法与站点、区域模拟实践技术应用
开发语言·python