0

is it possible to iterat through dictionary like this?

  1. I want to count all dictionary items (sum every Value),
  2. Next, for each Key I want to take their Value
  3. Then, I want to divide every EACH Key Value with sum
  4. And lasty I want to multiply every output from 3

I made it so it works with 1 item, but not sure how to make it so it works with every item in dictionary.

Here is sample code for 1 item:

var dic = new Dictionary<string, int>();
//this is sum
double y = 0;
foreach (var item in dic)
{
 y += item.Value;
}
//this is selected item
double x = 0;
foreach (var item in dic)
{ 
 if (item.Key == "Smith")
 {
 x = item.Value;
 }
}
double z = 0;
z = x / y;
Console.WriteLine("Smith/DicSum: " + z);

Now I would like to multiply the Z's (each Z's for each key in dictionary).

I was thiking about making one big loop for this like:

for (int i=0; i<y; i++) where y is the sum for all items in dictionary and multiply z's on the end of the loop

but I still don't know how to grab all seperate values and divide them while not saying the specific key for each of them.

@edit Thanks for answears but check my edit. I have a string list, let's say

"Smith is very cool member of Smith Company"

And my program is counting the number of Smith's, so it will show x as two. Then I want to divide snumber of smiths (two) by number of all words, so it's 2/8 = 0,25. Then I want to do this with every word and multiply it so it will be 2/8*1/8*...*1/8 in this example. SO I want to multiply by a preious number from loop (from dictionary), and not by a fixed amount, that's what making this problem.

Hamid Pourjam
20.8k9 gold badges61 silver badges75 bronze badges
asked Nov 2, 2014 at 11:07
1
  • 1
    Your entire second loop is equivalent to dic.TryGetValue("Smith", out x); Commented Nov 2, 2014 at 11:14

1 Answer 1

2
var words = "Smith is very cool member of Smith Company"
 .Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
var dic = new Dictionary<string, double>();
foreach (string word in words)
{
 if (dic.ContainsKey(word))
 dic[word]++;
 else
 dic[word] = 1;
}
var sum = dic.Sum(x => x.Value);
var result = dic.Values.Aggregate(1.0, (current, item) => current * (item / sum));
answered Nov 2, 2014 at 11:28
Sign up to request clarification or add additional context in comments.

3 Comments

Hey, check my edit :) because in your example you multiply by fixed value not the previous loop value, sorry if i asked my question wrong.
Thanks, looking at this, it should work, but the value of result is 0,0 (checked in debugging), the sum is fine and it's 8. Don't know how to get into current/item tho.
I have edited the answer, my fault, the initial value should be 1.0 not 0.0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.