Calculate average in Dictionary <string, List<double>> C# - c#

I need calculate the average of a List into a dictionary, I´m start using this syntax to access to the double List and gets the average but the compiler give an error, I´m new working with dictionarys, how is the correct syntax to access each double List and gets the average?
Here is the example of my code:
Dictionary<string, List<double>> SignalValues =
new Dictionary<string, List<double>>();
var dSignalAvg = SignalValues
.Where(x => x.Key == "Key")
.Average(x => x.Value);
Thank you so much!

If you are sure that the specific key exists in the dictionary then you can simply do:
var dSignalAvg = SignalValues["Key"].Average();
The code above will throw an exception if the dictionary doesn't have a key that equals "Key". If you want to check first, you can use TryGetValue like this:
double dSignalAvg = 0;
if (SignalValues.TryGetValue("Key", out List<double> signalList))
{
dSignalAvg = signalList.Average();
}

Like this?
Given a dictionary like this:
Dictionary<string,List<double>> values = new Dictionary<string,List<double>>();
Getting the average for a specific key is easy:
double avgValue = values
.Where( x => x.Key == "Key")
.Select( x => x.Value.Average() )
.FirstOrDefault()
;
Or, if you want to reduce your dictionary to a dictionary of averages, it's just:
Dictionary<string,double> avgValues = values
.ToDictionary(
x => x.Key ,
x => x.Value.Average()
)
;

Related

Efficient way to create a new list based of the differences in values in 2 dictionaries?

I currently have 2 strings that are formatted as an XML that are later converted into dictionaries for comparison.
So, I have a 2 Dictionary<string, object>, dict1 and dict2, that I need to compare. I need to:
Add the key to a list of strings if the values of these two dictionaries do not match
Add the key of dict2 to the list if dict1 does not contain this key
Currently, I have a simple foreach loop
foreach (string propName in dict2.Keys)
{
string oldDictValue;
string newDicValue = dict1[propName].ToString();
if (dict1.ContainsKey(propName))
{
oldDictValue = dict2[propName].ToString();
if (oldDictValue != newDicValue)
list.Add(propName);
}
else
{
list.Add(propName);
}
}
I would like to a faster solution to this problem if possible?
I don't claim that this is any faster, but it should be on par and it's less code:
List<string> list =
dict2
.Keys
.Where(k => !(dict1.ContainsKey(k) && dict1[k].Equals(dict2[k])))
.ToList();
I did do some testing with this:
List<string> list =
dict2
.Keys
.AsParallel()
.Where(k => !(dict1.ContainsKey(k) && dict1[k].Equals(dict2[k])))
.ToList();
That produced a significantly faster run.
Here's how I produced my test data:
var dict1 = Enumerable.Range(0, 10000000).Select(x => Random.Shared.Next(2000000)).Distinct().ToDictionary(x => x.ToString(), x => (object)Random.Shared.Next(20));
var dict2 = Enumerable.Range(0, 10000000).Select(x => Random.Shared.Next(2000000)).Distinct().ToDictionary(x => x.ToString(), x => (object)Random.Shared.Next(20));
You could make it faster by avoiding to get separately the dict1[propName] and the dict2[propName]. You could get the value along with the key, either by enumerating directly the KeyValuePairs stored in the dictionary, or by calling the TryGetValue method:
foreach (var (key, value2) in dict2)
{
if (!dict1.TryGetValue(key, out var value1)
|| value1.ToString() != value2.ToString())
{
list.Add(key);
}
}

Convert ForEach Dictionary Value If Condition And Add Key to List

May I know how can I convert the following statement to LINQ?
Dictionary<Guid, Guid> BatchID_To_RunID = new Dictionary<Guid, Guid>();
List<Guid> BatchIDList_InCase = new List<Guid>();
foreach (var x in BatchID_To_RunID)
{
if (x.Value == RunID)
{
Guid BatchID = x.Key;
BatchIDList_InCase.Add(x.Key);
}
}
Thank you
Well, the if is effectively a filter, suggesting a Where call, and the use of x.Key is effectively a projection, suggesting a Select call. So assuming BatchIDList_InCase is empty before this loop, you could replace it with:
// TODO: Rename BatchID_To_RunID to follow .NET naming conventions
var batchIds = BatchID_To_RunID
.Where(pair => pair.Value == RunID)
.Select(pair => pair.Key)
.ToList();
Or using a query expression:
var batchIds = (from pair in BatchID_To_RunID where pair.Value == RunID select pair.Key)
.ToList();
So you have a dictionary BatchID_To_RunID and a variable RunId
Requirement
Give me the keys of all items in the dictionary that have a Value equal to RunId.
If you write it precisely like this, the solution is not very difficult:
GUID RunId = ...
var result = BatchID_To_RunID // this is a sequence of KeyValuePair<GUID, GUID>
// Keep only those KeyValuePairs with a value equal to RunId
.Where(keyValuePair => keyValuePair.Value == RunId)
// from the remaining keyValuePairs, Select the Key
.Select(keyValuepair => keyValuePair.Key)
// and convert this sequence of GUIDs into a List:
.ToList();
Simple comme bonjour!
This is one way to do it:
BatchIDList_InCase.AddRange(BatchID_To_RunID.Where(b => b.Value == RunID).Select(b => b.Key).ToList());

LinQ ofType in Value

I have the following Dictionary:
public Dictionary<string,object> Items;
Now I need to get all Items where the Value of the Dictionary-item is from a specific type. (e.g. "int")
var intValues = Items.OfType<KeyValuePair<string,int>> simply does not work.
Code without LinQ would be something like:
var intValues=new Dictionary<string,int>()
foreach (var oldVal in Items) {
if (oldVal.Value is int) {
intValues.add(oldVal.Key, oldVal.Value);
}
}
(Update) my example should show the basic idea. But if possible I would avoid to create a new Dictionary as a result.
The direct translation of your foreach would be the following in LINQ:
var intValues = Items.Where(item => item.Value is int)
.ToDictionary(item => item.Key, item => (int)item.Value);
So basically, you filter first for where the item.Value is an int, and then you create a dictionary from it using ToDictionary where you cast those values to int to make sure that the resulting dictionary is a Dictionary<string, int>. Since we filtered non-integers already, this type cast will always succeed.
You can use the is operator on the Value property:
var intValues = Items.Where(x => x.Value is int);
If you want an actual Dictionary<string,int> at the end just add:
.ToDictionary(v=> v.Key, v=> (int)v.Value)
Try with this:
var intValue = Items
.Where(x => x.Value is int) // filter per Value is int
.ToDictionary(x => x.Key, x => (int)x.Value); // create a new dictionary converting Value to int
You can do
var result = Items.Where(x => x.Value is int)
.ToDictionary(x => x.Key, x => x.Value);

compare two dictionaries by key to obtain values of matching keys

I have two dictionaries A and B.
A - (a,b) (c,d) (e,f)
B - (a,p) (c,q) (g,h)
I want to be able to make a new dictionary C which will be as below -
C - (b,p) (d,q)
Is there any way I can do this?
This is what I currently have:
var C= B.Where(d => A.ContainsKey(d.Key)).ToList();
Easy with Linq ;)
var query =
from x in dictionary1
join y in dictionary2 on x.Key equals y.Key
select new { Value1 = x.Value, Value2 = y.Value };
var newDict = query.ToDictionary(item => item.Value1, item => item.Value2);
However it's not the most efficient approach, since it doesn't take advantage of the dictionary's fast lookup. A faster approach would be something like this:
var newDict = new Dictionary<string, string>(); // adjust the key and value types as needed
foreach (var kvp in dictionary1)
{
string value2;
if (dictionary2.TryGetValue(kvp.Key, out value2))
{
newDict.Add(kvp.Value, value2);
}
}

Convert List<MyObject> to Dictionary <obj.string, List<obj.ID>>

I would like to take a list of objects and convert it to a dictionary where the key is a field in the object, and the value is a list of a different field in the objects that match on the key. I can do this now with a loop but I feel this should be able to be accomplished with linq and not having to write the loop. I was thinking a combination of GroupBy and ToDictionary but have been unsuccessful so far.
Here's how I'm doing it right now:
var samplesWithSpecificResult = new Dictionary<string, List<int>>();
foreach(var sample in sampleList)
{
List<int> sampleIDs = null;
if (samplesWithSpecificResult.TryGetValue(sample.ResultString, out sampleIDs))
{
sampleIDs.Add(sample.ID);
continue;
}
sampleIDs = new List<int>();
sampleIDs.Add(sample.ID);
samplesWithSpecificResult.Add(sample.ResultString, sampleIDs);
}
The farthest I can get with .GroupBy().ToDictionay() is Dictionary<sample.ResultString, List<sample>>.
Any help would be appreciated.
Try the following
var dictionary = sampleList
.GroupBy(x => x.ResultString, x => x.ID)
.ToDictionary(x => x.Key, x => x.ToList());
The GroupBy clause will group every Sample instance in the list by its ResultString member, but it will keep only the Id part of each sample. This means every element will be an IGrouping<string, int>.
The ToDictionary portion uses the Key of the IGrouping<string, int> as the dictionary Key. IGrouping<string, int> implements IEnumerable<int> and hence we can convert that collection of samples' Id to a List<int> with a call to ToList, which becomes the Value of the dictionary for that given Key.
Yeah, super simple. The key is that when you do a GroupBy on IEnumerable<T>, each "group" is an object that implements IEnumerable<T> as well (that's why I can say g.Select below, and I'm projecting the elements of the original sequence with a common key):
var dictionary =
sampleList.GroupBy(x => x.ResultString)
.ToDictionary(
g => g.Key,
g => g.Select(x => x.ID).ToList()
);
See, the result of sampleList.GroupBy(x => x.ResultString) is an IEnumerable<IGrouping<string, Sample>> and IGrouping<T, U> implements IEnumerable<U> so that every group is a sequence of Sample with the common key!
Dictionary<string, List<int>> resultDictionary =
(
from sample in sampleList
group sample.ID by sample.ResultString
).ToDictionary(g => g.Key, g => g.ToList());
You might want to consider using a Lookup instead of the Dictionary of Lists
ILookup<string, int> idLookup = sampleList.ToLookup(
sample => sample.ResultString,
sample => sample.ID
);
used thusly
foreach(IGrouping<string, int> group in idLookup)
{
string resultString = group.Key;
List<int> ids = group.ToList();
//do something with them.
}
//and
List<int> ids = idLookup[resultString].ToList();
var samplesWithSpecificResult =
sampleList.GroupBy(s => s.ResultString)
.ToDictionary(g => g.Key, g => g.Select(s => s.ID).ToList());
What we 're doing here is group the samples based on their ResultString -- this puts them into an IGrouping<string, Sample>. Then we project the collection of IGroupings to a dictionary, using the Key of each as the dictionary key and enumerating over each grouping (IGrouping<string, Sample> is also an IEnumerable<Sample>) to select the ID of each sample to make a list for the dictionary value.

Categories