标签: ordereddictionary

OrderedDictionary和Dictionary

我正在寻找一种方法来按照添加它们的顺序Dictionary枚举它KeyValuePair.现在,Dictionary的文档明确指出:

出于枚举的目的,字典中的每个项目都被视为KeyValuePair<TKey, TValue>表示值及其键的结构.返回项的顺序未定义.

我发现我需要的是一个OrderedDictionary,但我是怀疑论者,我决定亲自尝试:

OrderedDictionary od = new OrderedDictionary();
Dictionary<String, String> d = new Dictionary<String, String>();

for (int i = 0; i < 10; i++)
{
    od.Add("key" + i, "value" + i);
    d.Add("key" + i, "value" + i);
}

System.Console.WriteLine("OrderedDictionary");
foreach (DictionaryEntry de in od) {
    System.Console.WriteLine(de.Key + ", " + de.Value);
}

System.Console.WriteLine("Dictionary");
foreach (var tmp in d) {
    System.Console.WriteLine(tmp.Key + ", " + tmp.Value);
}
Run Code Online (Sandbox Code Playgroud)

输出:

OrderedDictionary
key0, …
Run Code Online (Sandbox Code Playgroud)

c# dictionary ordereddictionary

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

为什么OrderedDict的值不相等?

使用Python 3:

>>> from collections import OrderedDict
>>> d1 = OrderedDict([('foo', 'bar')])
>>> d2 = OrderedDict([('foo', 'bar')])
Run Code Online (Sandbox Code Playgroud)

我想检查是否平等:

>>> d1 == d2
True
>>> d1.keys() == d2.keys()
True
Run Code Online (Sandbox Code Playgroud)

但:

>>> d1.values() == d2.values()
False
Run Code Online (Sandbox Code Playgroud)

你知道为什么价值观不平等吗?

我用Python 3.4和3.5进行了测试.


在这个问题之后,我发布了Python-Ideas邮件列表以获得更多详细信息:

https://mail.python.org/pipermail/python-ideas/2015-December/037472.html

python dictionary ordereddictionary python-3.x

50
推荐指数
3
解决办法
3210
查看次数

如何对OrderedDict的OrderedDict进行排序 - Python

我试图通过'depth'键在OrderedDict中对OrderedDict进行排序.是否有任何解决方案来排序该词典?

OrderedDict([
  (2, OrderedDict([
    ('depth', 0),  
    ('height', 51), 
    ('width', 51),   
    ('id', 100)
  ])), 
  (1, OrderedDict([
    ('depth', 2),  
    ('height', 51), 
    ('width', 51),  
    ('id', 55)
  ])), 
  (0, OrderedDict([
    ('depth', 1),  
    ('height', 51), 
    ('width', 51),  
    ('id', 48)
  ])),
]) 
Run Code Online (Sandbox Code Playgroud)

排序的dict应如下所示:

OrderedDict([
  (2, OrderedDict([
    ('depth', 0),  
    ('height', 51), 
    ('width', 51),   
    ('id', 100)
  ])), 
  (0, OrderedDict([
    ('depth', 1),  
    ('height', 51), 
    ('width', 51),  
    ('id', 48)
  ])),
  (1, OrderedDict([
    ('depth', 2),  
    ('height', 51), 
    ('width', 51),  
    ('id', 55)
  ])), 
]) 
Run Code Online (Sandbox Code Playgroud)

任何想法如何得到它?

python sorting nested ordereddictionary

49
推荐指数
3
解决办法
6万
查看次数

Python OrderedDict不保持元素顺序

我正在尝试创建一个OrderedDict对象但是我刚创建它,而不是元素都混乱.

这就是我做的:

from collections import OrderedDict
od = OrderedDict({(0,0):[2],(0,1):[1,9],(0,2):[1,5,9]})
Run Code Online (Sandbox Code Playgroud)

元素不会按照我指定的顺序保留

od
OrderedDict([((0, 1), [1, 9]), ((0, 0), [2]), ((0, 2), [1, 5, 9])])
Run Code Online (Sandbox Code Playgroud)

docs.python.org没有一个例子,我无法弄清楚为什么订单变得混乱.任何帮助是极大的赞赏.

python ordereddictionary python-2.7

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

覆盖{...}符号所以我得到一个OrderedDict()而不是一个dict()?

我想像配置文件一样使用.py文件.因此,使用{...}符号我可以使用字符串作为键创建字典,但定义顺序在标准python字典中丢失.

我的问题:是否有可能覆盖{...}符号,以便我得到一个OrderedDict()而不是dict()

我希望用OrderedDict(dict = OrderedDict)简单地覆盖dict构造函数会起作用,但事实并非如此.

例如:

dict = OrderedDict
dictname = {
   'B key': 'value1',
   'A key': 'value2',
   'C key': 'value3'
   }

print dictname.items()
Run Code Online (Sandbox Code Playgroud)

输出:

[('B key', 'value1'), ('A key', 'value2'), ('C key', 'value3')]
Run Code Online (Sandbox Code Playgroud)

python dictionary overriding ordereddictionary built-in

41
推荐指数
6
解决办法
2万
查看次数

OrderedDict中的最后一个元素

我有od类型OrderedDict.我想访问它最近添加的(键,值)对.od.popitem(last = True)会这样做,但也会删除od我不想要的那对.

有什么好办法呢?可以/应该这样做:

class MyOrderedDict(OrderedDict):
  def last(self):
    return next(reversed(self))
Run Code Online (Sandbox Code Playgroud)

python ordereddictionary python-3.x

36
推荐指数
3
解决办法
2万
查看次数

如何知道Python有序字典中项目的位置

我们能否知道Python有序字典中项目的位置?例如:

如果我有字典:

// Ordered_dict is OrderedDictionary

Ordered_dict = {"fruit": "banana", "drinks": "water", "animal": "cat"}
Run Code Online (Sandbox Code Playgroud)

现在如何知道猫属于哪个位置?是否有可能得到如下答案:

cat 或以其他方式?

python ordereddictionary

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

使用列表作为DataGridView的数据源

我已经从配置文件中将设置名称及其各自的值提取到有序字典中.字典包含ICollection类的键和值.我想绑定该数据并将其显示在DataGridView中.我已经尝试将字符串复制到数组并显示这些数组,但是当我运行程序时,列是空白的,它似乎根本没有绑定.

我还尝试将DataGridView源直接设置为有序字典集合(键或值),但这也没有产生我想要的任何东西; 列仍然是空白的.但是,第三列的列名称为"length",并显示ICollection中条目的长度.但不用说我不想要长度,我想要条目本身.

这是我用于此问题的代码:在加载表单时,我加载配置文件,名为m_Settings的私有成员具有所有键值对.然后我创建一个列表并分别添加键和值.将绑定源设置为'data'后,我运行程序,我添加的两个列都是空白的.

    private void Form4_Load(object sender, EventArgs e)
    {
        loadconfigfile(Properties.Settings.Default.Config);
        List<Object> data = new List<Object>();
        data.Add(m_Settings.Keys);
        data.Add(m_Settings.Values);
        bindingSource1.DataSource = data;
        dataGridView1.DataSource = bindingSource1;
        dataGridView1.Refresh();
    }
Run Code Online (Sandbox Code Playgroud)

关于如何获得有序字典并在标有"设置"和"值"的两列中显示条目的任何想法?我相信列表是兼容DataGridViews的DataSources,但现在我开始猜测自己了.

任何帮助或方向非常感谢!如果需要,我很乐意提供更多信息.

谢谢!

编辑:

以下是已实现的myStruct类的修订代码:

    List<myStruct> list = new List<myStruct>();
    for(int index = 0; index < m_Settings.Count; index++)
    {
        myStruct pair = new myStruct(keys[index], values[index].ToString());
        list.Add(pair);
    }

    public class myStruct
    {
        private string Key { get; set; }
        private string Value { get; set; }

        public myStruct(string key, string value)
        {
            Key …
Run Code Online (Sandbox Code Playgroud)

c# datasource datagridview ordereddictionary icollection

30
推荐指数
2
解决办法
17万
查看次数

Python OrderedDict迭代

为什么我的python OrderedDict被初始化为'乱序'?

这里的解决方案不如解释那么有趣.这里有些东西,我只是没有得到,也许扩张会帮助别人和我.

>>> from collections import OrderedDict

>>> spam = OrderedDict(s = (1, 2), p = (3, 4), a = (5, 6), m = (7, 8))

>>> spam
OrderedDict([('a', (5, 6)), ('p', (3, 4)), ('s', (1, 2)), ('m', (7, 8))])

>>> for key in spam.keys():
...    print key    
...
#  this is 'ordered' but not the order I wanted....
a
p
s
m

# I was expecting (and wanting):
s
p
a
m
Run Code Online (Sandbox Code Playgroud)

python ordereddictionary

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

如何子类化OrderedDict?

对Python进行子类化dict按预期工作:

>>> class DictSub(dict):
...     def __init__(self):
...         self[1] = 10
...         
>>> DictSub()
{1: 10}
Run Code Online (Sandbox Code Playgroud)

但是,用a做同样的事情是collections.OrderedDict行不通的:

>>> import collections
>>> class OrdDictSub(collections.OrderedDict):
...     def __init__(self):
...         self[1] = 10
...         
>>> OrdDictSub()
(…)
AttributeError: 'OrdDictSub' object has no attribute '_OrderedDict__root'
Run Code Online (Sandbox Code Playgroud)

因此,OrderedDict实现使用私有__root属性,这可以防止子类OrdDictSub的行为类似于DictSub子类.为什么?如何从OrderedDict继承?

python inheritance subclass ordereddictionary

28
推荐指数
1
解决办法
5285
查看次数