使用索引从分词中提取键和值 [英] extracting keys and values from disctionary using an index

查看:152
本文介绍了使用索引从分词中提取键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用其索引从字典数组中提取键和值?
例如:

How do you extract the key and value from an dictionary array using its index?
ex:

Dictionary<int, double> ListPPM = new Dictionary<int, double>();
           ListPPM.Add(1, 445.1242);
           ListPPM.Add(4, 445.1992);
           ListPPM.Add(7, 445.1205);
           ListPPM.Add(22, 445.1205);
           ListPPM.Add(32, 445.1242);
           ListPPM.Add(34, 445.1992);
           ListPPM.Add(100, 445.1205);
           ListPPM.Add(157, 445.1205);



说我想要ListPPM [2],它的键为:7,445.1205



say I want ListPPM[2] which has key:7,445.1205

推荐答案

这可能会有所帮助,

在C#中解析通用词典的键/值 [ ^ ]

:)
It might be helpful,

Parse Key/Value of a Generic Dictionary in C#[^]

:)


我不确定这里的索引.如果您以不同顺序将项目添加到列表中会怎样?您想获得第三小的钥匙吗?我之所以这样问,是因为 SortedDictionary [
I''m not sure about the index here. What happens if you add the items into the list in different order? Do you want to get the third smallest key? Why I''m asking this is because SortedDictionary[^] guarantees that the items are sorted by the key.

So one possibility, if you want to use Dictionary, is to query the dictionary using LINQ and define the sorting in the statement. Something like:
var q = from b in
               (from a in ListPPM
                orderby a.Key
                select a).Skip(2)
            select b;

q.First().Key;


您可以按以下顺序通过键集合中的字典序来访问字典的键:

you can access a dictionary''s key by its ordinal in the key collection with the following:

int index = 3;//or whatever you want
ListPPM.Keys.ElementAt<int>(index);



需要System.Linq命名空间

帖子格式搞乱了通用类型



requires System.Linq namespace

EDIT : post format was messing up generic type


这篇关于使用索引从分词中提取键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆