Python中文
首页
教程
问答
标签
搜索
登录
注册
python`list`和`for`返回不同的结果
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>为什么<code>list</code>和<code>for</code>在函数生成的值上使用它们时会报告不同的结果?你知道吗</p> <pre><code>from collections import deque def neighbours(comp0, cand0): comp = deque([i for i in comp0]) cand = deque([i for i in cand0]) for i in range(len(cand)): elem = cand.popleft() comp.append(elem) yield comp, cand comp.pop() cand.append(elem) return >>> n = neighbours([2], [1,4,5]) >>> list(n) [(deque([2]), deque([1, 4, 5])), (deque([2]), deque([1, 4, 5])), (deque([2]), deque([1, 4, 5]))] >>> n = neighbours([2], [1,4,5]) >>> for i in n: ... print(i) ... (deque([2, 1]), deque([4, 5])) (deque([2, 4]), deque([5, 1])) (deque([2, 5]), deque([1, 4])) >>> </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>哈维尔已经很好地解释了这个问题的原因。现在,如果希望使用迭代得到相同的结果,可以使用:</p> <pre><code>>>> [i for i in n] [(deque([2]), deque([1, 4, 5])), (deque([2]), deque([1, 4, 5])), (deque([2]), deque([1, 4, 5]))] </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
想用靓汤抢夺价值
10 回答
想申请一份符合工作描述的简历吗
10 回答
想画网格,Python
7 回答
想白痴化我的Python战舰
3 回答
想看两列日期,但只上
6 回答
想看看我写的这个脚本读一个Fortran二进制fi吗
6 回答
想知道Django是如何实现ORM查询优化的吗
4 回答
想知道GeoDjango和地图服务吗
1 回答
想知道Image.resize操作在PIL文件中的详细工作方式吗
9 回答
想知道matplotlib pyplot为什么不调整边距
3 回答
想知道matplotlib颜色的颜色代码吗
3 回答
想知道pd.factorize,pd.get_dummies,sklearn.preprocessing.labeencoder和OneHotEncod之间的区别
10 回答
想知道property()在python中的实际用法吗
9 回答
想知道pyodidejs是如何工作的吗?
3 回答
想知道pyparsing==2.0.1的已完成处理依赖关系
9 回答
想知道ScikitLearn中的编码算法吗
8 回答
想知道VTK 5.04和VTK 5.4.2的vtkMassProperties差异吗
5 回答
想知道一个特定字符在一个特定句子中出现的次数吗
4 回答
想知道两个不同子集的重叠中有多少个对象吗
3 回答
想知道为什么is_素数函数的结果不正确吗
2 回答