python中的高效列表操作(Efficient list manipulation in python)
我有一个很大的列表,并且经常需要找到一个满足相当复杂条件(不相等)的项目,即我被迫检查列表中的每个项目,直到找到一个。 条件发生变化,但有些项目比其他项目更频繁。 所以我想在每次找到匹配项目时将匹配项目带到列表的前面,这样可以更快地找到经常匹配的项目。
有没有一种有效的 pythonic方法来做到这一点?
序列(
[]
)由数组支持,因此删除中间某处的项目并将其添加到数组中意味着移动每个前一项目。 这是在O(n)时间,不好。在C中,您可以构建链接列表,并在找到时自行移动项目。 在Python中有一个
deque
,但是你不能引用节点对象,也不能访问.next
指针。Python中的自制链表非常慢。 (事实上,如果不移动任何项目,它比普通线性搜索慢。)
遗憾的是,
dict
或set
基于价值平等来查找项目,因此不适合我的问题。举个例子,这是条件:
u, v, w = n.value # list item if v in g[u] and w in g[v] and u not in g[w]: ...
I have a large list and regularily need to find an item satisfying a rather complex condition (not equality), i.e. I am forced to check every item in the list until I find one. The conditions change, but some items match more often then others. So I would like to bring the matching item to the front of the list each time I find one, so frequently matching items are found more quickly.
Is there an efficient, pythonic way to do this?
Sequences (
[]
) are backed by an array, so removing an item somewhere in the middle and prepending it to the array means moving every previous item. That's in O(n) time, not good.In C you could build a linked list and move the item on your own when found. In Python there is a
deque
, but afaik you cannot reference the node objects nor have access to.next
pointers.And a self-made linked list is very slow in Python. (In fact it's slower than ordinary linear search without moving any item.)
Sadly, a
dict
orset
finds items based on value equality and thus doesn't fit my problem.As an illustration, here's the condition:
u, v, w = n.value # list item if v in g[u] and w in g[v] and u not in g[w]: ...
原文:https://stackoverflow.com/questions/28004021
相关问答
更多-
这个问题也给出了更好的答案。 但实质上,从包中获取sublime code intel,然后当你按CTRL + R ,你可以跳转到你选择的任何方法。 This question also gives a better answer. But in essence, get sublime code intel from packages, and then when you press CTRL + R, you can just jump to any method of your choice.
-
命令:5:20 将插入符号重新定位到字符20前面的第5行(没有突出显示)。 这实际上是自3080年以来的内置功能,也适用于3103(经过测试)。 确保第5行至少包含20个字符。 如果少于,则插入符号跳到下一行。 并确保按Enter键确认命令。 在输入时已经选择了该行,但是对于该列,您需要使用回车确认。 The command :5:20 repositions the caret to line 5 in front of character 20 (there is no highlighting). T ...
-
Sublime Text 2:卸载自定义插件的sublime命令(Sublime Text 2: sublime commands from custom plugin are unloaded)[2022-05-15]
我发现命令是可见的总是它应该是sublime_plugin.WindowCommand的继承者:)在我的情况下,它是从sublime_plugin.TextCommand派生的,这就是为什么在没有打开文件时它不可见的原因。 I have found that for command to be visible always it should be a successor of sublime_plugin.WindowCommand :) In my case it was derived from su ... -
我知道这是一个老问题,原始海报询问Sublime Text 2,但我刚刚为自己解决了类似的问题,Sublime Text 3提供了一种方便的方法。 如果您定义了模块级plugin_loaded函数,Sublime Text 3将在您的插件加载时执行您的代码。 我写了一篇关于此的博客文章,其中包含更多细节和一些示例代码 。 I know this is an old question and the original poster was asking about Sublime Text 2, but I ...
-
有一个进行中的Sublime Text包连接到clang以获取名为SublimeClang的自动填充数据我还没有设法成功使它与Cocoa / UIKit Dev完全兼容,但这里有一个截图 和我的选择 ,这是一个开始 There is an in-progress Sublime Text package that connects to clang to get autocomplete data called SublimeClang I've not managed to successfully ge ...
-
Sublime Text - 添加标题(Sublime Text - Add a header)[2023-11-08]
我认为它与开箱即用的Windows不兼容,当我尝试安装它时,我在Sublime的控制台中出现了这个错误: Traceback (most recent call last): File "C:\Program Files\Sublime Text 3\sublime_plugin.py", line 550, in run_ return self.run(edit, **args) File "C:\Users\Thibaut\AppData\Roaming\Sublime Text ... -
如何为Laravel 4 Artisan安装Sublime Text插件?(How to install Sublime Text plugin for Laravel 4 Artisan?)[2022-12-10]
首先如果你没有包控制安装它ctrl +`并粘贴它: 对于SUBLIME 3 import urllib.request,os,hashlib; h = 'eb2297e1a458f27d836c04bb0cbaf282' + 'd0e7a3098092775ccb37ca9d6b2e4b7d'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_ope ... -
Sublime Text 3 PHP UNIT(Sublime Text 3 PHP UNIT)[2023-11-28]
我强烈建议您使用包裹控制中没有的此包: 崇高,PHPUnit的 您需要通过导航到~/Library/Application Support/Sublime Text 3/Packages并将其粘贴进行手动拉动。如果您设置了一些键绑定,则工作流程非常快捷。 观看此视频了解更多信息: 即时PHPUnit反馈和工作流程 I strongly suggest you to use this package which isn't available on Package Control: Sublime-PHPUn ... -
如何从`sublime console`运行WindowCommand插件(How to run WindowCommand plugin from `sublime console`)[2024-01-24]
ApplicationCommand : sublime.run_command('application_command_name') 。 检查API参考中 sublime模块的run_command函数。 WindowCommand : window.run_command('window_command_name') 。 检查run_command方法。 TextCommand : view.run_command('text_command_name') 。 检查run_command方法。 App ... -
如何永久启用Sublime Text 3 LiveReload插件(How to permanently enable Sublime Text 3 LiveReload plugin)[2023-09-02]
我安装了: https : //github.com/Grafikart/ST3-LiveReload 下一页:首选项 - >打包设置 - > LiveReload - >设置 - 用户 加: { "enabled_plugins": [ "SimpleRefresh" ] } I installed: https://github.com/Grafikart/ST3-LiveReload Next: preferences -> Packge Settings -> L ...