标签: ordereddictionary

有序字典的深度复制是否期望保留其顺序?

我编写了一个小程序并测试它确实保持了顺序。然而,我仍然想确保deepcopy能够做到这一点。

import copy
import collections

a_dict = collections.OrderedDict()
a_dict['m'] = 10
a_dict['u'] = 15
a_dict['c'] = 5
a_dict['h'] = 25
a_dict['a'] = 55
a_dict['s'] = 30

print(a_dict)

other_dict = copy.deepcopy(a_dict)

other_dict['g'] = 75
other_dict['r'] = 35

print(other_dict)
Run Code Online (Sandbox Code Playgroud)

该程序的输出是

OrderedDict([('m', 10), ('u', 15), ('c', 5), ('h', 25), ('a', 55), ('s', 30)])
OrderedDict([('m', 10), ('u', 15), ('c', 5), ('h', 25), ('a', 55), ('s', 30), ('g', 75), ('r', 35)])
Run Code Online (Sandbox Code Playgroud)

python ordereddictionary deep-copy

5
推荐指数
1
解决办法
5637
查看次数

在.NET中有没有Word的替代品?

我需要在项目中使用字典,但我们知道它们只能使用键访问而不使用索引,我想使用索引访问字典中的项目.所以我在网上乱搞并发现OrderedDictionary,因为它们可以使用索引和键访问,但它们有一些性能问题,因为我每天都在读/写字典,所以使用OrderedDictionary不是一个好主意.

所以最后我的问题是,是否有任何替代方案可以为我提供Dictionary的功能,我也可以使用索引访问它,并且不会导致性能问题.

.net c# performance dictionary ordereddictionary

4
推荐指数
1
解决办法
3106
查看次数

按密钥长度排序的字典

任何人都知道如何按密钥长度对这本字典进行排序?

{
    'http://ccc.com/viewvc/' : [{'type': 'web-servers', 'app': 'Apache', 'ver': '2.2.14'}, {'type': 'operating-systems', 'app': 'Ubuntu', 'ver': None}],    
    'http://bbb.com/' : [{'type': 'web-servers', 'app': 'Apache', 'ver': '2.2.22'}, {'type': 'programming-languages', 'app': 'PHP', 'ver': '5.3.10'}, {'type': 'cms', 'app': 'Drupal', 'ver': None}, {'type': 'operating-systems', 'app': 'Ubuntu', 'ver': None}, {'type': 'javascript-frameworks', 'app': 'jQuery', 'ver': None}, {'type': 'captchas', 'app': 'Mollom', 'ver': None}]
}
Run Code Online (Sandbox Code Playgroud)

预期产量:

{
    'http://bbb.com/' : [{'type': 'web-servers', 'app': 'Apache', 'ver': '2.2.22'}, {'type': 'programming-languages', 'app': 'PHP', 'ver': '5.3.10'}, {'type': 'cms', 'app': 'Drupal', 'ver': None}, {'type': 'operating-systems', 'app': …
Run Code Online (Sandbox Code Playgroud)

python sorting dictionary ordereddictionary

4
推荐指数
2
解决办法
3018
查看次数

如何测试秩序意识的哈希平等

Ruby 1.9.2将顺序引入哈希.考虑到顺序,我如何测试两个哈希值是否相等?

鉴于:

h1 = {"a"=>1, "b"=>2, "c"=>3}
h2 = {"a"=>1, "c"=>3, "b"=>2}
Run Code Online (Sandbox Code Playgroud)

我想要一个返回falsefor h1和的比较运算符h2.以下两点都不起作用:

h1 == h2 # => true
h1.eql? h2 # => true
Run Code Online (Sandbox Code Playgroud)

ruby hash equality ordereddictionary

4
推荐指数
1
解决办法
976
查看次数

使用自然下标创建三维OrderedDict

我想要一个具有以下属性的字典结构:

  1. 双嵌套(所以,这么多单词都是三维的)
  2. 记住每个级别添加到其中的内容的顺序

所以,如果我像这样添加项目:

# d = something dict-ish
d['a']['b']['c'] = 'd'
d['a'][1][2] = 3
d['f']['g']['e'] = 'g'
d['f'][5][6] = 7
d['a']['foo']['bar'] = 'hello world'
Run Code Online (Sandbox Code Playgroud)

以下理解的结果:

[(i, j, k, d[i][j][k]) for i in d for j in d[i] for k in d[i][j]]
Run Code Online (Sandbox Code Playgroud)

将会:

[('a', 'b', 'c', 'd'), ('a', 1, 2, 3), ('a', 'foo', 'bar', 'hello world'), ('f', 'g', 'e', 'g'), ('f', 5, 6, 7)]
Run Code Online (Sandbox Code Playgroud)

我已经尝试使用a defaultdict为新键强制执行此结构,因此我不必长途键入它,如下所示:

# long way
d = OrderedDict()
d['a'] = OrderedDict([('b', OrderedDict([('c', 'd')]))])
d['a'][1] = …
Run Code Online (Sandbox Code Playgroud)

python ordereddictionary python-2.7 defaultdict

4
推荐指数
1
解决办法
452
查看次数

如何计算物品的数量,但保持它们出现的顺序?

例如,我需要计算一个单词出现在列表中的次数,不按频率排序,而是按单词出现的顺序排序,即插入顺序.

from collections import Counter

words = ['oranges', 'apples', 'apples', 'bananas', 'kiwis', 'kiwis', 'apples']

c = Counter(words)

print(c)
Run Code Online (Sandbox Code Playgroud)

所以代替: {'apples': 3, 'kiwis': 2, 'bananas': 1, 'oranges': 1}

我宁愿得到: {'oranges': 1, 'apples': 3, 'bananas': 1, 'kiwis': 2}

我真的不需要这种Counter方法,任何可以产生正确结果的方法对我来说都是可以的.

python counter dictionary ordereddictionary python-3.x

4
推荐指数
2
解决办法
1779
查看次数

用OrderedDict替换对象的默认__dict__

我的代码中有两个类.firstsecond继承的父.

class first(object):
    def __init(self,**kwargs):  
        pass

    def __setattr__(self,name,value):
        self.__dict__[name] = value

class second(first):
    def do_something(self):
        self.a = 1
        self.b = 2
        self.c = 3
Run Code Online (Sandbox Code Playgroud)

当我打印第二类(例如second.__dict__)时,我得到无序字典.这很明显.我想更改此行为以使用OrderedDict该类获取有序字典,但它不起作用.我正在first通过以下方式更改实现:

class first(OrderedDict):   
    def __init__(self,**kwargs):  
        super(first,self).__init__(**kwargs)  
    def __setattr__(self,name_value):  
        super(first,self).__setattr__(name_value)  
Run Code Online (Sandbox Code Playgroud)

我想second__dict__或打印__repr__,但我得到了无序字典.我应该改变什么?

python dictionary ordereddictionary

4
推荐指数
2
解决办法
4529
查看次数

OrderedDict 子项的深层副本

我已经尝试过deepcopyfromcopy模块。它适用于 OrderedDict 实例和 dict child 实例。但它不适用于 OrderedDict 子实例。这是一个演示:

from collections import OrderedDict
from copy import deepcopy

class Example2(dict):
    def __init__(self,l):
        dict.__init__(self,l)

class Example3(OrderedDict):
    def __init__(self,l):
        OrderedDict.__init__(self,l)

d1=OrderedDict([(1,1),(2,2)]) 
print(deepcopy(d1))           #OrderedDict([(1, 1), (2, 2)])

d2=Example2([(1,1),(2,2)])
print(deepcopy(d2))           #{1: 1, 2: 2}

d3=Example3([(1,1),(2,2)])
print(deepcopy(d3))
Run Code Online (Sandbox Code Playgroud)

前两个示例按预期工作,但最后一个因异常而崩溃:

TypeError: __init__() missing 1 required positional argument: 'l'
Run Code Online (Sandbox Code Playgroud)

所以问题是:这里的实际问题是什么,是否可以在deepcopy这种情况下使用该功能?

python ordereddictionary

4
推荐指数
1
解决办法
5884
查看次数

python按时间顺序排序OrderedDict键

我有以下内容OrderedDict:

 from collections import OrderedDict
 a = OrderedDict()
 a['2016:April'] = 1
 a['2016:January'] = 2
 a['2017:February'] = 3
 a['2015:November'] = 4
Run Code Online (Sandbox Code Playgroud)

我想按时间顺序按键排序字典,结果如下:

 OrderedDict([('2015:November', 4), ('2016:January', 2), ('2016:April', 1), ('2017:February', 3)])
Run Code Online (Sandbox Code Playgroud)

python sorting dictionary ordereddictionary

4
推荐指数
1
解决办法
1133
查看次数

simple_salesforce python中的父子关系查询,从有序的dicts中提取

我正在尝试使用simple_salesforcepython中的包查询salesforce中的信息.

问题是,它是一个嵌套字段,它是父子关系的一部分,成为有序字典中的有序字典

我想要从Opportunity对象中找到id,以及与该记录关联的accountid.

SOQL查询可能看起来像......

query = "select id, account.id from opportunity where closedate = last_n_days:5"
Run Code Online (Sandbox Code Playgroud)

在SOQL(salesforce对象查询语言)中,点表示数据库中的父子关系.所以我试图从机会对象获取id,然后从该记录上的帐户对象获取相关的id.

出于某种原因,Id很好,但是account.id嵌套在有序字典中的有序字典中:

q = sf.query_all(query)
Run Code Online (Sandbox Code Playgroud)

这会拉回一个有序的字典..

OrderedDict([('totalSize', 455),
             ('done', True),
             ('records',
              [OrderedDict([('attributes',
                             OrderedDict([('type', 'Opportunity'),
                                          ('url',
Run Code Online (Sandbox Code Playgroud)

我会拉records一块ordereddict来创造一个df

df = pd.DataFrame(q['records'])
Run Code Online (Sandbox Code Playgroud)

这给了我3列,一个有序的dict调用'attributes',Id另一个有序的dict调用'Account'.我正在寻找一种('BillingCountry', 'United States')从嵌套的有序字典中提取出来的方法'Account'

[OrderedDict([('attributes',
               OrderedDict([('type', 'Opportunity'),
                            ('url',
                             '/services/data/v34.0/sobjects/Opportunity/0061B003451RhZgiHHF')])),
              ('Id', '0061B003451RhZgiHHF'),
              ('Account',
               OrderedDict([('attributes',
                             OrderedDict([('type', 'Account'),
                                          ('url',
                                           '/services/data/v34.0/sobjects/Account/001304300MviPPF3Z')])),
                            ('BillingCountry', 'United States')]))])
Run Code Online (Sandbox Code Playgroud)

编辑:澄清我在寻找什么.

我希望以一个数据框结束,每个查询字段都有一列.

当我'records'使用df = pd.DataFrame(sf.query_all(query)['records'])它将片段放入DataFrame时,它给了我:

attributes  Id …
Run Code Online (Sandbox Code Playgroud)

python salesforce ordereddictionary soql pandas

4
推荐指数
1
解决办法
1124
查看次数