Python中文
首页
教程
问答
标签
搜索
登录
注册
python字符串vs列表奇数行为
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>如果我在二叉树上尝试下面的代码片段,然后尝试打印arr和字符串,arr会给出正确的结果,但是字符串是空的。有什么想法吗?这与通过引用传递的列表和通过值传递的字符串有关吗?你知道吗</p> <pre><code>def post_order(root, arr = []): if(root is not None): post_order(root.left, arr) post_order(root.right, arr) arr.append(root.value) def post_order1(root, string = ''): if(root is not None): post_order1(root.left, string) post_order1(root.right, string) string += str(root.value) # assume I've made my binary tree arr, string = [], '' post_order(root, arr) post_order1(root, string) print arr, string # arr holds the correct post-order sequence # string is empty </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>在Python中,列表是可变的,字符串是不变的。这意味着可以修改列表,但不能修改字符串。只能重新分配字符串。你知道吗</p> <p>在函数中,您使用<code>.append()</code>修改列表,但只重新分配字符串<code>+=</code></p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
使用Python创建一个非常大的二进制频率矩阵来运行协作过滤
8 回答
使用Python创建一张HTML网页,其中在不同颜色中重复n遍显示“Hello World”的方法
2 回答
使用Python创建一组唯一的值length L
9 回答
使用python创建不同表格的透视表
7 回答
使用python创建不和谐频道
3 回答
使用python创建不存在的多个文件夹
6 回答
使用python创建串行远程文件
10 回答
使用python创建交互式仪表板时出现问题
2 回答
使用python创建交互式绘图
4 回答
使用python创建交互式自动电子邮件
9 回答
使用Python创建价格列表
7 回答
使用python创建修改的txt文件
7 回答
使用Python创建全局变量,初始化后更改值
2 回答
使用Python创建关键字搜索词数组
7 回答
使用Python创建具有不均匀块大小/堆叠条形图的热图
7 回答
使用Python创建具有依赖于另一列的值的列
9 回答
使用Python创建具有多列的HTML表
6 回答
使用Python创建具有时间范围数据的等距数据帧
2 回答
使用Python创建具有特定顺序或属性的XML文件
4 回答
使用Python创建具有级联功能的搜索栏
6 回答