我正在使用OrderedDict随机访问列表,但现在想要next列表中的项目来自我拥有的项目:
foo = OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])
apple = foo['apple']
Run Code Online (Sandbox Code Playgroud)
我如何使用foo和apple?
我不知道为什么这不起作用:
我正在使用PEP 372中的odict类,但我想将它用作成员,即:__dict__
class Bag(object):
def __init__(self):
self.__dict__ = odict()
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,我得到了奇怪的结果.这有效:
>>> b = Bag()
>>> b.apple = 1
>>> b.apple
1
>>> b.banana = 2
>>> b.banana
2
Run Code Online (Sandbox Code Playgroud)
但是尝试访问实际的字典不起作用:
>>> b.__dict__.items()
[]
>>> b.__dict__
odict.odict([])
Run Code Online (Sandbox Code Playgroud)
它变得更奇怪了:
>>> b.__dict__['tomato'] = 3
>>> b.tomato
3
>>> b.__dict__
odict.odict([('tomato', 3)])
Run Code Online (Sandbox Code Playgroud)
我感觉非常愚蠢.你能帮我吗?
我有一个OrderedDict按值排序的有序字典().如何获得最高(例如25个)键值并将它们添加到新词典中?例如:我有这样的事情:
dictionary={'a':10,'b':20,'c':30,'d':5}
ordered=OrderedDict(sorted(dictionary.items(), key=lambda x: x[1],reverse=True))
Run Code Online (Sandbox Code Playgroud)
现在ordered是一个有序的字典,我想创建一个字典,比如通过获取前2个最频繁的项目及其键:
frequent={'c':30,'b':20}
Run Code Online (Sandbox Code Playgroud) 显然,快速搜索会在Python中产生一百万个memoization装饰器的实现和风格.但是,我感兴趣的是一种我无法找到的味道.我希望它能够使存储值的缓存具有固定容量.添加新元素时,如果达到容量,则删除最旧的值并替换为最新值.
我担心的是,如果我使用memoization来存储很多元素,那么程序会因为缺少内存而崩溃.(我不知道这种担忧在实践中有多好.)如果缓存的大小固定,那么内存错误就不是问题.我工作的许多问题随着程序的执行而发生变化,因此初始缓存的值看起来与以后的缓存值非常不同(以后不太可能再次发生).这就是为什么我希望最新的东西被最新的东西取代.
我找到了这个OrderedDict类和一个示例,展示了如何将其子类化以指定最大大小.我想将它用作我的缓存,而不是正常dict.问题是,我需要memoize装饰器来获取一个名为maxlendefaults 的参数None.如果是None,那么缓存是无限的并且正常运行.任何其他值都用作缓存的大小.
我希望它像以下一样工作:
@memoize
def some_function(spam, eggs):
# This would use the boundless cache.
pass
Run Code Online (Sandbox Code Playgroud)
和
@memoize(200) # or @memoize(maxlen=200)
def some_function(spam, eggs):
# This would use the bounded cache of size 200.
pass
Run Code Online (Sandbox Code Playgroud)
下面是我到目前为止的代码,但是我没有看到如何将参数传递给装饰器,同时使它既可以"裸"又可以使用参数.
import collections
import functools
class BoundedOrderedDict(collections.OrderedDict):
def __init__(self, *args, **kwds):
self.maxlen = kwds.pop("maxlen", None)
collections.OrderedDict.__init__(self, *args, **kwds)
self._checklen()
def __setitem__(self, key, value):
collections.OrderedDict.__setitem__(self, key, value)
self._checklen()
def _checklen(self):
if self.maxlen is not None: …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用funnelweb附加组件从站点导入一些静态页面.这是我写的:bin/funnelweb --crawler:url=http://wiki.scandiatransplant.com --crawler:max=50 --ploneupload=http://admin:admin@localhost:8080/TestPage.
我收到此错误:
ImportError: cannot import name OrderedDict
Run Code Online (Sandbox Code Playgroud)
下面添加完整的回溯.有谁知道如何解决这个问题?
Traceback (most recent call last):
File "bin/funnelweb", line 116, in <module>
mr.migrator.runner.runner({},"funnelweb.remote")
File "/home/magiq/Plone/buildout-cache/eggs/mr.migrator-1.0.1-py2.6.egg/mr/migrator/runner/__init__.py", line 69, in runner
load_config('autoinclude.zcml', mr.migrator)
File "/home/magiq/Plone/buildout-cache/eggs/Zope2-2.13.12-py2.6.egg/Zope2/App/zcml.py", line 55, in load_config
_context = xmlconfig.file(config, package, _context, execute=execute)
File "/home/magiq/Plone/buildout-cache/eggs/zope.configuration-3.7.4-py2.6.egg/zope/configuration/xmlconfig.py", line 649, in file
include(context, name, package)
File "/home/magiq/Plone/buildout-cache/eggs/zope.configuration-3.7.4-py2.6.egg/zope/configuration/xmlconfig.py", line 548, in include
processxmlfile(f, context)
File "/home/magiq/Plone/buildout-cache/eggs/zope.configuration-3.7.4-py2.6.egg/zope/configuration/xmlconfig.py", line 380, in processxmlfile
parser.parse(src)
File "/home/magiq/Plone/Python-2.6/lib/python2.6/xml/sax/expatreader.py", line 107, in parse
xmlreader.IncrementalParser.parse(self, source)
File "/home/magiq/Plone/Python-2.6/lib/python2.6/xml/sax/xmlreader.py", line …Run Code Online (Sandbox Code Playgroud) 我找到了源代码,它似乎是 O(1),因为它基本上是一个链表和一个字典的更新。虽然我不确定。
你怎么认为?谢谢!
我想在一个给定的密钥后面插入一个密钥OrdedDict.
例:
my_orderded_dict=OrderedDict([('one', 1), ('three', 3)])
Run Code Online (Sandbox Code Playgroud)
我想'two' --> 2进入正确的地方.
在我的情况下,我需要更新OrdedDict就地.
背景
Django的SortedDict(有一个insert())被删除:https://code.djangoproject.com/wiki/SortedDict
如果字典中存在键,我想知道键的位置即数字索引.例如 :
如果字典包括:
{'test':{1,3},'test2':{2},'test3':{2,3}}
if 'test' in dictionary:
print(the index of that key)
Run Code Online (Sandbox Code Playgroud)
例如,输出为0.('test3'的输出为2 ......)
我现在正在使用字典,我猜我必须使用命令dict才能执行此操作,但我怎么能使用有序的dict呢?
谢谢你的帮助.
在python 2中使用时,OrderedDict我只需使用keys返回列表的方法就可以按插入顺序获取密钥.然而在python 3中:
rows = OrderedDict()
rows[0]=[1,2,3]
rows[1]=[1,2,3]
image = [rows[k] for k in rows.keys()[:2]]
Run Code Online (Sandbox Code Playgroud)
我明白了:
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'odict_keys' object is not subscriptable
Run Code Online (Sandbox Code Playgroud)
我当然可以list(rows)[:2]按照这里的建议进行操作- 但这是否可以保证按键排序?这是正确的方法吗?
更新:python 2代码会更好:
image = [v for v in rows.values()[:2]]
Run Code Online (Sandbox Code Playgroud)
当然在python 3上仍然会出现同样的错误