python - Python - 字符串列表列表
问题描述
所以,基本上我有一个队列(字符串列表列表)。现在,假设我想将“好的”附加到索引 1 处的列表中,为什么要为 Queue 中的所有列表附加它?如何仅在队列内将值附加到特定列表?
Queue = []
temp = []
Queue.append(temp) # 12 - 8
Queue.append(temp)
Queue.append(temp)
Queue[1].append('okay')
print(Queue)
[['okay'], ['okay'], ['okay']]
解决方案
Because the three items in Queue are not three different empty lists: they're all temp
. Thus temp
, Queue[0]
, Queue[1]
and Queue[2]
are all the same object and modifying one of them has the result of modifying them all.
Try the following code instead:
Queue = []
Queue.append([])
Queue.append([])
Queue.append([])
Queue[1].append('okay')
print(Queue)
推荐阅读
- excel - 如果满足两个条件,则执行操作
- python - setup.py 如何引用源代码库中的其他文件?
- javascript - 如何用 jquery 添加 ul 元素的样式?
- c# - C# ASP.Net Core 在表单提交失败后保留提交的文件
- python - android中的python套接字无法连接到在virtualbox中运行的python服务器
- elasticsearch - 过滤热门点击聚合未按预期工作
- javascript - mac Safari 上的 SpeechRecognition 事件监听器
- php - Apache 不发布 ram
- sql - Oracle DB 上的 Apache Drill Timestampdiff
- python - 如何为 Python 多处理中的每个 API 调用创建唯一进程 ID 以更好地记录日志