首页 \ 问答 \ python中的高效列表操作(Efficient list manipulation in python)

python中的高效列表操作(Efficient list manipulation in python)

我有一个很大的列表,并且经常需要找到一个满足相当复杂条件(不相等)的项目,即我被迫检查列表中的每个项目,直到找到一个。 条件发生变化,但有些项目比其他项目更频繁。 所以我想在每次找到匹配项目时将匹配项目带到列表的前面,这样可以更快地找到经常匹配的项目。

有没有一种有效的 pythonic方法来做到这一点?

序列( [] )由数组支持,因此删除中间某处的项目并将其添加到数组中意味着移动每个前一项目。 这是在O(n)时间,不好。

在C中,您可以构建链接列表,并在找到时自行移动项目。 在Python中有一个deque ,但是你不能引用节点对象,也不能访问.next指针。

Python中的自制链表非常慢。 (事实上​​,如果不移动任何项目,它比普通线性搜索慢。)

遗憾的是, dictset基于价值平等来查找项目,因此不适合我的问题。

举个例子,这是条件:

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 or set 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
更新时间:2023-10-28 13:10

相关问答

更多
  • 这个问题也给出了更好的答案。 但实质上,从包中获取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_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 ...
  • 我认为它与开箱即用的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 ...
  • 首先如果你没有包控制安装它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 ...
  • 我强烈建议您使用包裹控制中没有的此包: 崇高,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 ...
  • 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 ...
  • 我安装了: https : //github.com/Grafikart/ST3-LiveReload 下一页:首选项 - >打包设置 - > LiveReload - >设置 - 用户 加: { "enabled_plugins": [ "SimpleRefresh" ] } I installed: https://github.com/Grafikart/ST3-LiveReload Next: preferences -> Packge Settings -> L ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。