Create Permutations from Dictionary<string, List<string>>

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Assimalyst

    Create Permutations from Dictionary<string, List<string>>

    Hi

    I have a Dictionary<stri ng, List<string>>, which i have successfully
    filled. My problem is I need to create a filter expression using all
    possible permutations of its contents.

    i.e. the dictionary essentially creates the following array:

    Key Value

    Column1 1
    Column1 2
    Column2 17

    I want to then create an number of filter expressions:

    Column1 = 1 AND Column2 = 17
    Column1 = 2 AND Column2 = 17

    I used a permutation library i found PermuteUtils (

    ) and the following code:

    Dictionary<stri ng, List<string>dis tinctCellValues = new
    Dictionary<stri ng, List<string>>() ;
    ArrayList queryClause = new ArrayList();

    {
    foreach (KeyValuePair<s tring, List<string>kvp in
    distinctCellVal ues)
    {
    foreach (string value in kvp.Value)
    {
    queryClause.Add (kvp.Key + " = " + value);
    }
    }

    //Convert array to string array
    string[] queryClauseStri ng = queryClause.ToA rray(typeof(str ing)) as
    string[];

    CreateFilterPer mutations<strin g>(queryClauseS tring,
    distinctCellVal ues.Count);
    }

    private void CreateFilterPer mutations<T>(IE numerable<Tinpu t, int
    count)
    {
    foreach (IEnumerable<Tp ermutation in
    PermuteUtils.Pe rmute<T>(input, count))
    {
    foreach (T i in permutation)
    {
    filterExpressio ns.Add(i);
    }
    }
    }

    This seems to work OK as far as running the CreateFilterPer mutations
    method, where i just cannot get it to do what i need.

    I've manage to get myself very confused by all this, and this only
    contains 2 columns. Potentially i could need to be generating
    permutations of 10 columns/values or more!! I'm hoping someone can
    straighten out my thoughts before my head explodes!!

    Thanks
  • Fred Mellender

    #2
    Re: Create Permutations from Dictionary&lt;s tring, List&lt;string& gt;&gt;

    There is a good chance I don't understand your problem, but here is my take:

    I don't think you want the permutations of the array elements, but instead
    want a subset of the Cartesian product of the array with itself. E.G. if
    the array is A= [a, b, c], the Cartesian product A x A is
    {(a,a), (a,b), (a,c), (b,a), (b,b), (b,c), (c,a), (c,b), (c,c)}. The subset
    you want excludes an element whose coords are the same, and considers as
    duplicates 2 elements that have the same coords (e.g. (a,b) and (b,a) are
    the same).

    So, you want {(a,b), (a,c), (b,co)}

    If you only have a single array, X, crossed only once with itself,
    containing more than one element, some potential pseudo code would be:

    for (int i = 0; i < x.Length-1; i++)
    {
    for (int j = i+1; j < x.Length; j++)
    {
    myFilterList.Ad d(x[i].MyToString() + " AND " +
    x[j].MyToString())
    }
    }

    In your case, x is the list of KeyValue pairs from the dictionary, and you
    need to convert each KeyValue to a string via MyToString(), so as to form a
    filter, which you add to a list, myFilterList.

    What I don't understand about your example:
    You have the same key, Column1 associated with two different values. I
    don't think the Dictionary allows this.


    "Assimalyst " <c_oxtoby@hotma il.comwrote in message
    news:8c0f0921-c3a0-432f-a953-68e1a533d6e7@s8 g2000prg.google groups.com...
    Hi
    >
    I have a Dictionary<stri ng, List<string>>, which i have successfully
    filled. My problem is I need to create a filter expression using all
    possible permutations of its contents.
    >
    i.e. the dictionary essentially creates the following array:
    >
    Key Value
    >
    Column1 1
    Column1 2
    Column2 17
    >
    I want to then create an number of filter expressions:
    >
    Column1 = 1 AND Column2 = 17
    Column1 = 2 AND Column2 = 17
    >
    I used a permutation library i found PermuteUtils (

    ) and the following code:
    >
    Dictionary<stri ng, List<string>dis tinctCellValues = new
    Dictionary<stri ng, List<string>>() ;
    ArrayList queryClause = new ArrayList();
    >
    {
    foreach (KeyValuePair<s tring, List<string>kvp in
    distinctCellVal ues)
    {
    foreach (string value in kvp.Value)
    {
    queryClause.Add (kvp.Key + " = " + value);
    }
    }
    >
    //Convert array to string array
    string[] queryClauseStri ng = queryClause.ToA rray(typeof(str ing)) as
    string[];
    >
    CreateFilterPer mutations<strin g>(queryClauseS tring,
    distinctCellVal ues.Count);
    }
    >
    private void CreateFilterPer mutations<T>(IE numerable<Tinpu t, int
    count)
    {
    foreach (IEnumerable<Tp ermutation in
    PermuteUtils.Pe rmute<T>(input, count))
    {
    foreach (T i in permutation)
    {
    filterExpressio ns.Add(i);
    }
    }
    }
    >
    This seems to work OK as far as running the CreateFilterPer mutations
    method, where i just cannot get it to do what i need.
    >
    I've manage to get myself very confused by all this, and this only
    contains 2 columns. Potentially i could need to be generating
    permutations of 10 columns/values or more!! I'm hoping someone can
    straighten out my thoughts before my head explodes!!
    >
    Thanks

    Comment

    • Assimalyst

      #3
      Re: Create Permutations from Dictionary&lt;s tring, List&lt;string& gt;&gt;

      Thank you Fred.

      I think i had confused myself. Not sure I want permutations but rather
      combinations.

      Your code sample has given me some ideas that i think i can get
      working with a bit of expansion.
      I'll keep playing with it . . .

      By the way, i think you understood my problem exactly. The bit you
      didn't understand was just the way i represented it. I didn't have
      duplicate keys it was more like

      Column1 { 1, 2 }
      Column2 { 17 }

      Thanks again


      On Nov 30, 6:14 pm, "Fred Mellender" <nospamPlease_f red...@gmail.co m>
      wrote:
      There is a good chance I don't understand your problem, but here is my take:
      >
      I don't think you want the permutations of the array elements, but instead
      want a subset of the Cartesian product of the array with itself. E.G. if
      the array is A= [a, b, c], the Cartesian product A x A is
      {(a,a), (a,b), (a,c), (b,a), (b,b), (b,c), (c,a), (c,b), (c,c)}. The subset
      you want excludes an element whose coords are the same, and considers as
      duplicates 2 elements that have the same coords (e.g. (a,b) and (b,a) are
      the same).
      >
      So, you want {(a,b), (a,c), (b,co)}
      >
      If you only have a single array, X, crossed only once with itself,
      containing more than one element, some potential pseudo code would be:
      >
      for (int i = 0; i < x.Length-1; i++)
      {
      for (int j = i+1; j < x.Length; j++)
      {
      myFilterList.Ad d(x[i].MyToString() + " AND " +
      x[j].MyToString())
      }
      >
      }
      >
      In your case, x is the list of KeyValue pairs from the dictionary, and you
      need to convert each KeyValue to a string via MyToString(), so as to form a
      filter, which you add to a list, myFilterList.
      >
      What I don't understand about your example:
      You have the same key, Column1 associated with two different values. I
      don't think the Dictionary allows this.
      >
      "Assimalyst " <c_oxt...@hotma il.comwrote in message
      >
      news:8c0f0921-c3a0-432f-a953-68e1a533d6e7@s8 g2000prg.google groups.com...
      >
      Hi
      >
      I have a Dictionary<stri ng, List<string>>, which i have successfully
      filled. My problem is I need to create a filter expression using all
      possible permutations of its contents.
      >
      i.e. the dictionary essentially creates the following array:
      >
      Key Value
      >
      Column1 1
      Column1 2
      Column2 17
      >
      I want to then create an number of filter expressions:
      >
      Column1 = 1 AND Column2 = 17
      Column1 = 2 AND Column2 = 17
      >
      I used a permutation library i found PermuteUtils (
      http://www.koders.com/csharp/fid3768...EE8E638B71457A...
      ) and the following code:
      >
      Dictionary<stri ng, List<string>dis tinctCellValues = new
      Dictionary<stri ng, List<string>>() ;
      ArrayList queryClause = new ArrayList();
      >
      {
      foreach (KeyValuePair<s tring, List<string>kvp in
      distinctCellVal ues)
      {
      foreach (string value in kvp.Value)
      {
      queryClause.Add (kvp.Key + " = " + value);
      }
      }
      >
      //Convert array to string array
      string[] queryClauseStri ng = queryClause.ToA rray(typeof(str ing)) as
      string[];
      >
      CreateFilterPer mutations<strin g>(queryClauseS tring,
      distinctCellVal ues.Count);
      }
      >
      private void CreateFilterPer mutations<T>(IE numerable<Tinpu t, int
      count)
      {
      foreach (IEnumerable<Tp ermutation in
      PermuteUtils.Pe rmute<T>(input, count))
      {
      foreach (T i in permutation)
      {
      filterExpressio ns.Add(i);
      }
      }
      }
      >
      This seems to work OK as far as running the CreateFilterPer mutations
      method, where i just cannot get it to do what i need.
      >
      I've manage to get myself very confused by all this, and this only
      contains 2 columns. Potentially i could need to be generating
      permutations of 10 columns/values or more!! I'm hoping someone can
      straighten out my thoughts before my head explodes!!
      >
      Thanks

      Comment

      Working...