2013-12-13 24 views
1

還有就是Python代碼,將填補一個列表中給出一個k適應python的itertools.product內部在C#中

k=4 
myList = {} 
for objectOfInterest in [''.join(item) for item in product('01', repeat=k)]: 
    if objectOfInterest[:-1] in myList: 
     myList[objectOfInterest[:-1]].append(objectOfInterest[1:]) 
    else: 
     myList[objectOfInterest[:-1]] = [objectOfInterest[1:]] 

所得上:

k=3 
{'11': ['10', '11'], '10': ['00', '01'], '00': ['00', '01'], '01': ['10', '11']} 

k=4 
    {'010': ['100', '101'], '011': ['110', '111'], '001': ['010', '011'], '000': ['000', '001'], '111': ['110', '111'], '110': ['100', '101'], '100': ['000', '001'], '101': ['010', '011']} 


when k=5 
{'0110': ['1100', '1101'], '0111': ['1110', '1111'], '0000': ['0000', '0001'], '0001': ['0010', '0011'], '0011': ['0110', '0111'], '0010': ['0100', '0101'], '0101': ['1010', '1011'], '0100': ['1000', '1001'], '1111': ['1110', '1111'], '1110': ['1100', '1101'], '1100': ['1000', '1001'], '1101': ['1010', '1011'], '1010': ['0100', '0101'], '1011': ['0110', '0111'], '1001': ['0010', '0011'], '1000': ['0000', '0001']} 

我想將它翻譯成C#代碼 什麼是最好的方式,我認爲LINQ可以幫助...

int k =4; 
string myList =""; 

如何循環

objectOfInterest in [''.join(item) for item in product('01', repeat=k)]: 

看起來像c#中嗎?它是一個foraech item in objectOfInterest...知道事實的stackoverflow answer suggests

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; 
} 

注:這裏的struct表示T必須是值類型或結構。如果您需要拋出List等對象,請將其更改爲類,但要注意潛在的引用問題。

然後作爲一名車手:

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); 

輸出:

1, 7 
1, 8 
1, 9 
2, 7 
2, 8 
2, 9 
3, 7 
3, 8 
3, 9 
+3

我認爲_first_步驟是複製的功能['itertools.product'(http://docs.python.org/2/library/itertools。 HTML#itertools.product)。 – iCodez

+0

有suggestin'公共靜態列表< Tuple>產品(列表一個,列表 B) 其中T的答案:結構 { 列表>結果=新列表>(); (T t2 in a) foreach(t t2 in b) } 返回結果; }'... – cMinor

+0

然後'列表 = listA的新列表(){1,2,3}; List listB = new List (){7,8,9}; List > product = Product (listA,listB); foreach(Tuple 產品中的元組) Console.WriteLine(tuple.Item1 +「,」+ tuple.Item2);' – cMinor

回答

2

我最近寫的一類,有效地模擬itertools.product,通過微軟的面試問題提示。你可以抓住它here。目前它不支持repeat,但你可以模擬它。

拉東西放在一起:

//emulate the repeat step. http://stackoverflow.com/q/17865166/1180926 
List<List<char>> zeroOneRepeated = Enumerable.Range(0, k) 
    .Select(i => '01'.ToList()) 
    .ToList(); 

//get the product and turn into strings 
objectsOfInterest = CrossProductFunctions.CrossProduct(zeroOneRepeated) 
    .Select(item => new string(item.ToArray())); 

//create the dictionary. http://stackoverflow.com/a/938104/1180926 
myDict = objectsOfInterest.GroupBy(str => str.Substring(0, str.Length - 1)) 
    .ToDictionary(
     grp => grp.Key, 
     grp => grp.Select(str => str.Substring(1)).ToList() 
    ); 
相關問題