Python中文
首页
教程
问答
标签
搜索
登录
注册
相当于解救或重做的Python?
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我正在学习Python,我有一种情况,我想使用迭代器中的项。棘手的是,在某些条件下,我想“取消迭代”,也就是说,在循环之前将一个项放回迭代器的前面。</p> <p>例如,假设我在树上摘苹果。我的果篮只能装10公斤才需要倒空。但我得先摘下每一个苹果,然后再称重,确定这个苹果是否会超过篮子的容量。</p> <p>在Perl这样的语言中,我可以将apple放回树上,然后让循环表达式重新选择apple:</p> <pre><code>while ($apple = shift(@tree)) { $wt = weight($apple); if ($wt + weight(@basket) > 10) { send(@basket); @basket = (); unshift(@tree, $apple); } else { push(@basket, $element); } } </code></pre> <p>或者我也可以使用<code>redo</code>,它在块的顶部恢复处理,而不计算循环表达式。所以同样的苹果可以在篮子清空后重新加工。</p> <pre><code>while ($apple = shift(@tree)) { $wt = weight($apple); if ($wt + weight(@basket) > 10) { send(@basket); @basket = (); redo; } else { push(@basket, $apple); } } </code></pre> <p>对于这种问题,什么才是最有效的解决方案?</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>当else子句应该总是出现时,为什么要费心取消shift呢?</p> <pre><code>for apple in tree: if (apple.weight + basket.weight) > 10: send(basket) basket.clear() basket.add(apple) </code></pre> <p>不管怎样,我很确定Python没有你想要的那种行为。</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
VirtualEnvRapper错误:路径python2(来自python=python2)不存在
4 回答
virtualenvs上的pyinstaller,没有名为导入错误的模块
8 回答
virtualenvs是否可以退回到用户包而不是系统包?
8 回答
virtualenvwrapper CentOS7
5 回答
virtualenvwrapper IOError:[Errno 13]权限被拒绝
3 回答
virtualenvwrapper mkproject和shell在windows中的启动问题?
6 回答
virtualenvwrapper mkvirtualenv不工作但没有错误
7 回答
Virtualenvwrapper python bash
3 回答
virtualenvwrapper:“workon”何时更改到项目目录?
2 回答
virtualenvwrapper:mkvirtualenv可以工作,但是rmvirtualenv返回bash:没有这样的文件或目录
8 回答
virtualenvwrapper:virtualenv信息存储在哪里?
9 回答
virtualenvwrapper:命令“python设置.pyegg_info“失败,错误代码为1
7 回答
virtualenvwrapper:如何将mkvirtualenv的默认Python版本/路径更改为ins
9 回答
Virtualenvwrapper:模块“pkg_resources”没有属性“iter_entry_points”
10 回答
Virtualenvwrapper:没有名为virtualenvwrapp的模块
3 回答
Virtualenvwrapper.bash_profi的正确设置
7 回答
Virtualenvwrapper.hook:权限被拒绝
3 回答
virtualenvwrapper.sh:fork:资源暂时不可用Python/Djang
3 回答
Virtualenvwrapper.shlssitepackages命令不工作
8 回答
Virtualenvwrapper.sh函数在bash sh中不可用
4 回答