Splitting all dictionary values ββby value
I have the following code in C # where vector is the [string, double] dictionary type. I want to split all the values ββin this dictionary by the value "magnitude". Now my naive first code was as follows:
foreach (var key in vector.Keys)
{
vector[key] = vector[key] / magnitude;
}
Which throws an exception saying the collection has been modified in foreach. I can create a second dictionary to record the resulting values, but I don't want that.
Is there an easier way to do this, for example using methods that work with all values ββof the dictionary, such as the following:
vector.Values().Aggreagate(), vector.Values().Average()
source to share
The easiest way to do this is to just copy the list of keys before iterating:
foreach (var key in vector.Keys.ToList())
{
vector[key] = vector[key] / magnitude;
}
Or:
foreach (var entry in vector.ToList())
{
vector[entry.Key] = entry.Value / magnitude;
}
(This avoids double searches, but will of course copy more data.)
It's a bit of a shame that changing the value of an existing entry is treated as a change, which is admittedly preventing you from continuing to iterate over the keys.
An alternative would be to have a mutable wrapper type as value, then you could use:
foreach (var wrapper in vector.Values)
{
wrapper.Value = wrapper.Value / 10;
}
This will not modify the dictionary at all - just the objects that the dictionary refers to. I personally haven't done this most of the time, but it might be fine for you.
source to share
One of the simplest ways is to use a regular one for iteration, like this sample:
for (int i = 0; i < vector.Keys.Count; i++)
{
string key = vector.Keys.ElementAt(i);
vector[key] /= magnitude;
}
if you want to use Linq methods you can use this code:
vector=vector
.Select(x=> new KeyValuePair<string,double>(x.Key,x.Value/magnitude))
.ToDictionary( x=>x.Key,x=>x.Value);
source to share