改编python';s itertools.product inside for in c#

改编python';s itertools.product inside for in c#,c#,python,C#,Python,有一个python代码将填充给定k编号的列表 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[ob

有一个python代码将填充给定
k
编号的列表

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#?它是否是objectOfInterest中的科技项目…知道以下事实:


我最近写了一个类,它有效地模拟了微软的一个采访问题所提示的
itertools.product
。你可以抓住它。它目前不支持
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()
    );
//模拟重复步骤。http://stackoverflow.com/q/17865166/1180926
List zeroOneRepeated=可枚举。范围(0,k)
.Select(i=>'01'.ToList())
.ToList();
//获取产品并将其转换为字符串
objectsOfInterest=CrossProductFunctions.CrossProduct(zeroOneRepeated)
.Select(item=>newstring(item.ToArray());
//创建字典。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()
);

我认为第一步是复制的功能。在
公共静态列表产品(列表a,列表b)中有一个答案,其中T:struct{List result=new List();foreach(T t1 in a){foreach(T t2 in b)result.Add(Tuple.Create(t1,t2));}返回结果;}
…然后
List listA=new List(){1,2,3};List listB=新列表(){7,8,9};列表产品=产品(列表A、列表B);foreach(产品中的Tuple-Tuple)控制台.WriteLine(Tuple.Item1+“,”+Tuple.Item2)
使用linq
Zip
扩展实际上可以更清晰地完成您的最新示例,但我不确定它与您的原始问题有何关系。事实上,我想在for循环
中使用python的itertools.product功能,用于[''中的objectOfInterest.join(item)for product中的item('01',repeat=k)]:如果myList中的objectOfInterest[:-1]:myList[objectOfInterest[:-1]].append(objectOfInterest[1:])否则:myList[objectOfInterest[:-1]=[objectOfInterest[1:]
但不知道如何调整它
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
//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()
    );