Python中文
首页
教程
问答
标签
搜索
登录
注册
python deque/list过滤
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我有一个数据库中的操作列表。这被复制到deque中,因为我想按顺序处理它,在我走的时候从左边弹出一些东西。在</p> <p>所以我有类似actions=deque(actions)的东西,这很好。在</p> <p>每个操作项都是psycopg模块中使用DictCursor的一个列表。每个列表都有“阶段”项。在</p> <p>所以事情是分阶段的。有些操作在“a”阶段,有些在“b”阶段,等等。不是存储数据的最佳方式,但这就是我得到的结果。在</p> <p>因此,为了让我的生活更轻松,我想分阶段将deque分成几个deque。在</p> <p>所以如果actions[0]['phase']=='a',那么这将进入一个只包含阶段a的项目的列表中,以此类推,b等等</p> <p>我可以用一堆“如果”和“附加”来完成这项工作,但这似乎是一项很大的努力。我想答案可能是filter(),但我不太确定如何使用它。在</p> <p>随机注意事项:</p> <ul> <li>每件物品都是有序的,每件物品都需要保存。在</li> <li>这些阶段是已知的和连续的。例如,如果c相不存在,我们就知道d相不存在。相位是有限的,我记得大概是5个。在</li> </ul> <p>澄清尝试:</p> <p>我有个行为准则。比如:</p> <pre><code>actions = [ ['phase': 'a', 'something_else': 'x'], ['phase': 'a', 'something_else': 'y'], ['phase': 'b', 'something_else': 'x'] ] </code></pre> <p>想以(类似的)结尾:</p> ^{pr2}$ <p>只需最少的代码,并且可以在任何数量的阶段/项目中工作/等等</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>我想你需要itertools模块中的groupby。在</p> <p><a href="http://docs.python.org/library/itertools.html#itertools.groupby" rel="nofollow">http://docs.python.org/library/itertools.html#itertools.groupby</a></p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
当用户用PYTHON设置一个或一个不带值的URL时,他们怎么能输入一个/a的代码呢?
5 回答
当用户登录到站点时,如何显示不同的导航栏
4 回答
当用户登录时,在Flask中向用户显示处理结果
1 回答
当用户的Flask会话结束时,我如何从Redis后端中移除所有Celery结果?
4 回答
当用户的Okta配置文件字段当前为blan时,更新该字段
3 回答
当用户的付款逾期2天时,从Django模型检索数据
4 回答
当用户的消息以问号结尾时,如何让机器人说些什么?
2 回答
当用户的系统上可能也安装了Python 2.7时,如何在用户的系统上运行Python 3脚本?
2 回答
当用户确定打印数量时,使用Matplotlib打印动画
10 回答
当用户离开时是否可以删除整个网页?
9 回答
当用户给出一个单词时如何打印?
5 回答
当用户继续更改TKin中的值(使用trace方法)时,使用Entry并更新输入的条目
3 回答
当用户编辑表单字段时,从Django时间字段中删除秒数
4 回答
当用户被更改时,消息不会来自web套接字
3 回答
当用户访问表单时,如何使表单为只读,而不具有更改权限
7 回答
当用户试图更改对象的值时,使用描述符类引发RuntimeError
3 回答
当用户调整GUI的大小时,是否有方法更改GUI内容的大小?
8 回答
当用户调整风的大小时,pythontkinter小部件的大小会不均匀
5 回答
当用户购买某个类别时,是否查找其他类别的销售?
8 回答
当用户转到上一页时,Django和芹菜插入操作
7 回答