-2

I am working with python, and I am implementing my code to c# and in python there is method "product", anyone knows if there is something similar in c#? if not maybe someone can put me on a track of how to write this function by myself?

example of product:

a=[[[(1, 2), (3, 4)], [(5, 6), (7, 8)], [(9, 10), (11, 12)]], [[(13, 14), (15, 16)]]]

b= product(*a)

output:

([(1, 2), (3, 4)], [(13, 14), (15, 16)])
([(5, 6), (7, 8)], [(13, 14), (15, 16)])
([(9, 10), (11, 12)], [(13, 14), (15, 16)])
noodles
  • 19
  • 1
  • 4
  • 1
    Give example of the `product` method. – Fendy May 16 '13 at 11:18
  • 1
    Perhaps you could explain what the "product" method does in Python? It's not just a simply multiply funciton I take it? – BarrieK May 16 '13 at 11:18
  • 1
    Presumably the OP is referring to [the `product` function in `itertools`](http://docs.python.org/2/library/itertools.html#itertools.product), but they should clarify. – LukeH May 16 '13 at 11:22

3 Answers3

1

Assuming you mean itertools.product (it looks like it from the example given):

public static List< Tuple<T, T> > Product<T>(List<T> a, List<T> b)
    where T : struct
{
    List<Tuple<T, T>> result = new List<Tuple<T, T>>();

    foreach(T t1 in a)
    {
        foreach(T t2 in b)
            result.Add(Tuple.Create<T, T>(t1, t2));
    }

    return result;
}

n.b. struct here means that T must be a value type or a structure. Change it to class if you need to throw in objects such as Lists, but be aware of potential referencing issues.

Then as a driver:

List<int> listA = new List<int>() { 1, 2, 3 };
List<int> listB = new List<int>() { 7, 8, 9 };

List<Tuple<int, int>> product = Product<int>(listA, listB);
foreach (Tuple<int, int> tuple in product)
    Console.WriteLine(tuple.Item1 + ", " + tuple.Item2);

Output:

1, 7
1, 8
1, 9
2, 7
2, 8
2, 9
3, 7
3, 8
3, 9
Elle
  • 3,695
  • 1
  • 17
  • 31
0

For a product function valid over number of lists, you can use my CrossProductFunction.CrossProduct code here:

List<List<Tuple<int>>> a = new List<List<Tuple<int>>> { /*....*/ }
IEnumerable<List<Tuple<int>>> b = CrossProductFunctions.CrossProduct(a)

Currently, it doesn't accept repeat argument like itertools.product does, but it is otherwise similar in functionality and design.

Arithmomaniac
  • 4,604
  • 3
  • 38
  • 58
-3

Here is syntax for writing function in c#:

public void product()
        {
          ..........
          .........
        }

Link:

http://www.dotnetspider.com/forum/139241-How-write-function-c-.net.aspx

Can get information about all kinds of functions in c# on this link.

Freelancer
  • 9,008
  • 7
  • 42
  • 81
  • And how does this answer the question? – jAC May 16 '13 at 11:21
  • 1
    he simply asked "how to write function by myself?" – Freelancer May 16 '13 at 11:21
  • 1
    You forgot the word "this" in the sentence, which implies he meant hwo to write the function `product()` and not functions in general. – jAC May 16 '13 at 11:26
  • 2
    yes, this is product function which i have written. nothing wrong in it. – Freelancer May 16 '13 at 11:27
  • 2
    I don't agree with that. He want's to have the Python function() of `itertools.product()` in C# (a conversion). Your whole method body is missing, which is the most important and difficult task. – jAC May 16 '13 at 11:31
  • not mentioned anywhere in his question about 'itertools.product()' thats what every ones comment is asking him below the question. – Freelancer May 16 '13 at 11:33
  • 1
    OP wouldn't have talked about porting Python to C#, explained the product function, and asked "if there is something similar in c#?" if they only wanted to know how to write C# functions. – Elle May 16 '13 at 13:41