简体   繁体   English

清单 <double> 用于字典查找特定项的值

[英]List<double> for value of dictionary lookup specific item

I have a Dictionary with 5 different doubles in the list. 我有一个词典,列表中有5个不同的双打。 I know the order of each item in the List. 我知道列表中每个项目的顺序。 I am trying to find a one liner piece of code where I can lookup a specific value in my list given the key. 我试图找到一个单一的代码段,我可以在给定键的情况下在列表中查找特定值。

So something like: 所以像这样:

double myDbl = myDict["key"][value[3]];

Is something like this possible, I cant find it anywhere. 这是可能的,我在任何地方都找不到。 Thanks 谢谢

As others have said, if this is a Dictionary<string, List<double>> you could just use 正如其他人所说,如果这是Dictionary<string, List<double>> ,则可以使用

double value = myDict["key"][3];

However, this line: 但是,这一行:

I know the order of each item in the List. 我知道列表中每个项目的顺序。

makes me think that actually you should restructure your code. 让我认为实际上您应该重组代码。 If you know that the first item always represents one piece of data (eg weight), the next always represents another (eg height) etc, then you should just create a new type with those properties. 如果您知道第一个项目始终代表一个数据(例如重量),第二个项目始终代表另一个数据(例如高度)等,那么您应该使用这些属性创建一个新类型。 Using a List<double> for this will make the code much harder to maintain... it doesn't naturally reveal the information about what each value means. 为此,使用List<double>将使代码难以维护……它自然不会揭示有关每个值含义的信息。

Once you've changed it to, say, a Dictionary<string, Person> you can use: 一旦将其更改为Dictionary<string, Person> ,就可以使用:

double height = myDict["key"].Height;

which is significantly clearer. 这显然更加清晰。

Of course it's possible that you meant something else by the line I've quoted... 当然,您可能会在我引述的那行中表达其他意思...

Assuming you have a dictionary defined as 假设您有一个定义为的字典

Dictionary<string,List<double>>

And assuming the dictionary elements are initialized correctly you can do the following: 并假设正确初始化了字典元素,您可以执行以下操作:

myDbl = myDict["key"][3];

It should probably be something like: 可能应该是这样的:

double myDbl = myDict["key"][3];

As you're looking for index 3 from the resulting list from the key 当您从key的结果列表中寻找索引3时

By understanding the order in which the operators are executed, you can see that myDict["key"] will return your value type, which here is assumed to be List<double> . 通过了解运算符的执行顺序,您可以看到myDict["key"]将返回您的值类型,这里假定为List<double> From this return value, you then want to get the 4th object (0 based) so you use [3] at the end. 然后,您需要从该返回值获取第四个对象(从0开始),因此最后使用[3]

Given: 鉴于:

Dictionary<string, List<double>> l_dict = new Dictionary<string, List<double>>();
l_dict.Add( "key", new List<double>( new double[]{ 1, 2, 3, 4, 5 } );

Then: 然后:

List<double> l_valueCollection = l_dict["key"];
double l_value = l_valueCollection[3];

Or, more succinctly (the "one-liner" you wanted): 或者,更简洁(您想要的“单线”):

double l_value = l_dict["key"][3];

If your collection looks like this: 如果您的收藏集看起来像这样:

Dictionary<string,List<double>> myDictionary;

Then the following will work: 然后,以下将起作用:

double myDouble = myDictionary["key"][3];

Create a method like this that returns your value, no matter what key you pass in. 创建这样的方法,无论您传入什么键,它都将返回您的值。

    private double GetValue(Dictionary<string, double> dict, string key)
    {
        return (from pair in dict where pair.Key == key select pair.Value).FirstOrDefault();
    }

Remember that this is using LINQ and will only work with .Net Framework 3.5 请记住,这使用的是LINQ,并且仅适用于.Net Framework 3.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM