相关疑难解决方法(0)

如何根据任意条件函数过滤字典?

我有点词典,说:

>>> points={'a':(3,4), 'b':(1,2), 'c':(5,5), 'd':(3,3)}
Run Code Online (Sandbox Code Playgroud)

我想创建一个新的字典,其中包含x和y值小于5的所有点,即点'a','b'和'd'.

根据该书,每个字典都有items()函数,它返回一个(key, pair) 元组列表:

>>> points.items()
[('a', (3, 4)), ('c', (5, 5)), ('b', (1, 2)), ('d', (3, 3))]
Run Code Online (Sandbox Code Playgroud)

所以我写了这个:

>>> for item in [i for i in points.items() if i[1][0]<5 and i[1][1]<5]:
...     points_small[item[0]]=item[1]
...
>>> points_small
{'a': (3, 4), 'b': (1, 2), 'd': (3, 3)}
Run Code Online (Sandbox Code Playgroud)

有更优雅的方式吗?我期待Python有一些超级棒的dictionary.filter(f)功能......

python dictionary filter

190
推荐指数
7
解决办法
18万
查看次数

访问Python dict的时间复杂度

我正在编写一个简单的Python程序.

我的程序似乎受到字典线性访问的影响,即使算法是二次的,它的运行时间也呈指数级增长.
我使用字典来记忆值.这似乎是一个瓶颈.

我正在散列的值是点的元组.每个点是:(x,y),0 <= x,y <= 50
字典中的每个键是:2-5点的元组:((x1,y1),(x2,y2),(x3, Y3),(X4,Y4))

密钥的读取次数比写入次数多很多次.

我是否认为python dicts受到这些输入的线性访问时间的影响?

据我所知,集合保证了对数访问时间.
如何在Python中使用集合(或类似的东西)模拟dicts?

编辑根据请求,这是memoization函数的(简化)版本:

def memoize(fun):
    memoized = {}
    def memo(*args):
        key = args
        if not key in memoized:
            memoized[key] = fun(*args)
        return memoized[key]
    return memo
Run Code Online (Sandbox Code Playgroud)

python hash complexity-theory dictionary

31
推荐指数
4
解决办法
7万
查看次数

从python中不在列表中的对象中删除键?

我有这些钥匙:

keep = ["a","c"]
Run Code Online (Sandbox Code Playgroud)

我的词:

testdict = {'
'a':'vala',
'b':'valb',
'c':'valc',
'd':'vald'
}
Run Code Online (Sandbox Code Playgroud)

期望的输出:

testdict = {
'a':'vala',
'c':'valc'
}
Run Code Online (Sandbox Code Playgroud)

我想删除与列表中的键不匹配的所有键.最快的方法是什么?

我试过了:

for key, value in testdict.iteritems():
      if key not in keep:
         del testdict[key]
Run Code Online (Sandbox Code Playgroud)

但是由于尺寸变化,上面给出了错误.

python

9
推荐指数
2
解决办法
6495
查看次数

如何有效地排出oneliner中的迭代器?

如果我有一个迭代器it并且想要耗尽它我可以写:

for x in it:
    pass
Run Code Online (Sandbox Code Playgroud)

是否有内置或标准的库调用,允许我在单行中进行?我当然可以这样做:

list(it)
Run Code Online (Sandbox Code Playgroud)

这将从迭代器构建一个列表然后丢弃它.但我认为由于列表构建步骤效率低下.为自己编写一个帮助函数来执行空的for循环当然是微不足道的但是如果我还缺少其他东西我很好奇.

python

9
推荐指数
2
解决办法
896
查看次数

如何过滤dict以仅包含给定列表中的键?

这两者都是Python和stackoverflow的新手.感谢您的耐心和帮助.

我想根据列表的内容过滤一个字典,如下所示:

d={'d1':1, 'd2':2, 'd3':3}

f = ['d1', 'd3']

r = {items of d where the key is in f}
Run Code Online (Sandbox Code Playgroud)

这是荒谬的吗?如果没有,那么正确的语法是什么?

谢谢您的帮助.

文森特

python dictionary list filter

8
推荐指数
2
解决办法
1万
查看次数

从字典创建类实例?

我试图从字典创建类实例,其中包含多个类具有属性的键.我已经从这个链接读了相同问题的答案:从字典创建类实例属性?.问题是我无法__init__在我想要的类定义中编写,因为我使用的是SQLAlchemy声明式样式类定义.还会type('className', (object,), dict)创建不需要的错误属性.这是我找到的解决方案:

dict = {'key1': 'value1', 'key2': 'value2'}
object = MyClass(**dict)
Run Code Online (Sandbox Code Playgroud)

但是如果dict有冗余密钥,它就不起作用:

dict = {'key1': 'value1', 'key2': 'value2', 'redundant_key': 'redundant_value'}
object = MyClass(**dict) # here need to ignore redundant_key
Run Code Online (Sandbox Code Playgroud)

除了直接删除所有冗余密钥外,还有其他解决方案dict吗?

python dictionary sqlalchemy class

6
推荐指数
1
解决办法
4849
查看次数

从给定列表中出现的键中提取子字典

考虑一下我有一本看起来像这样的字典:

{1=>a, 2=>b, 3=>c, 4=>d}

和一个看起来像这样的列表:

[1, 2, 3]

有没有一种方法可以返回一个仅包含的子词典

{1=>a, 2=>b, 3=>c}

python python-2.7 python-3.x

2
推荐指数
1
解决办法
6172
查看次数

参数不兼容时字典解包成函数

我不确定这是否可能,我想将字典解压缩到函数中,但我没有类似的参数。是否可以轻松地将字典限制为子集?

def foo(a=0, b=0, c=0):
    print("a=%s, b=%s, c=%s"%(a,b,c))

my_dict = {'a':10, 'b':20, 'd':40}
foo(**my_dict)
Run Code Online (Sandbox Code Playgroud)

输出

TypeError                                 Traceback (most recent call last)
<ipython-input-1-d40731664736> in <module>()
      3 
      4 my_dict = {'a':10, 'b':20, 'd':40}
----> 5 foo(**my_dict)

TypeError: foo() got an unexpected keyword argument 'd'
Run Code Online (Sandbox Code Playgroud)

我想获得

a=10, b=20, c=0
Run Code Online (Sandbox Code Playgroud)

'd' 被自动拒绝的结果。

这只是一个例子。在我的情况下,该函数不是我的,我无法重新定义她的参数,foo 是不可修改的。

在与 foo() 类似的情况下,我有很多函数,这些函数是对象的方法。

有任何想法吗?

python dictionary

2
推荐指数
1
解决办法
56
查看次数

根据键值从字典中删除值

data = {'one': '1', 'two': '2', 'three': '3', 'four': '4', 'five': '5'}

keys = ('one', 'four')

unwanted = set(keys) - set(data)
for unwanted_key in unwanted: del data[unwanted_key]
Run Code Online (Sandbox Code Playgroud)

我想要的输出是:

data = {'two': '2', 'three': '3', 'five': '5'}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

我正在使用这个接受的答案的代码.

DEMO

python dictionary

0
推荐指数
1
解决办法
105
查看次数