linq - C# Efficiently replacing -Infinity values in a dictionary -


i have code uses linq converts dictionary of string, ints string, doubles. following code works fine:

public static void main(string[] args) {     dictionary<string, int[]> ret = new dictionary<string, int[]>();      int[] = {1,2,0,4,5};     int[] b = { 0, 6, 9, 0, 12 };     int[] c = {2,0,3,5,0};     ret.add("al", a);     ret.add("adam", b);     ret.add("axel", c);      dictionary<string, double[]> scores = ret.todictionary(r=> r.key,                          r => r.value.select((v, index)=>                           3 * math.log10((double)v / 10)                          ).toarray());       foreach (var item in scores)     {         (int = 0; < item.value.length; ++)         {             console.writeline("key = {0}, value = {1}", item.key, item.value[i]);         }      } 

this code outputs:

key = al, value = -3 key = al, value = -2.09691001300806 key = al, value = -infinity key = al, value = -1.19382002601611 key = al, value = -0.903089986991944 key = adam, value = -infinity key = adam, value = -0.665546248849069 key = adam, value = -0.137272471682025 key = adam, value = -infinity key = adam, value = 0.237543738142874 key = axel, value = -2.09691001300806 key = axel, value = -infinity key = axel, value = -1.56863623584101 key = axel, value = -0.903089986991944 key = axel, value = -infinity 

what efficient way change -infinity 0? putting continue or if statement function in loop work? know can use replace function , loop through dictionary, not efficient.

since have control of values being put dictionary, i'd change

(v, index) => 3 * math.log10((double)v / 10) 

to

(v, index) => v == 0 ? 0 : 3 * math.log10((double)v / 10) 

otherwise, can use ternary operator:

console.writeline("key = {0}, value = {1}", item.key,     item.value[i] == double.negativeinfinity ? 0 : item.value[i]); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -