我想做这样的事情但是在c#:
for element in [self.squares[i:i + 3] for i in range(0, len(self.squares), 3)]: // this is in python.
因为输出结果如下:
[0,0,0]
[0,0,0]
[0,0,0]
我怎么能在C#中做到这一点?我知道矩阵更好,但我想代表一个Tic Tac Toe板,我认为用python方式做得更好,因为我想找到实现Min-Max算法的邻接。
答案 0 :(得分:3)
你可以用一种简单的方式做到这一点
/project/design
和另一个版本,如果你有一个长度为9的列表(可能是tic tac toe数据)
public static void Print(int[][] matrix)
{
foreach (var row in matrix)
Console.WriteLine($"[{string.Join(",", row)}]");
}
答案 1 :(得分:1)
我尝试使一些类似于python外观的功能。还有一些东西可以让它看起来更相似,但从目前为止我所能看到的将涉及一些lambda functions,这可能有点过于杀戮。如果需要,可以测试运行here。
using System;
using System.Collections.Generic;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
List<int> matrix = MakeList(0,0,0,0,0,0,0,0,0);
foreach(var element in Matrixify(3, matrix))
Console.WriteLine(ArrayToString(element));
}
// Used to make list easier...
public static List<int> MakeList(params int[] elements)
{
return new List<int>(elements);
}
// Used for testing display.
public static string ArrayToString(int[] arr)
{
return "[" + string.Join(",", arr) + "]";
}
// Breaks a list into a matrix where delta is the # columns and # rows = len(list) / delta.
public static int[][] Matrixify(int delta, List<int> list)
{
// Get a range of start index points.
int[] arr = range(0, len(list), delta);
// Used to make the matrix.
List<int[]> result = new List<int[]>();
// Chops up into rows.
foreach(var i in arr)
result.Add(getRow(i, i + delta, list));
// Sends back as an array of arrays (aka matrix).
return result.ToArray();
}
// Grabs a row of the list passed in.
public static int[] getRow(int start, int stop, List<int> list)
{
return list.GetRange(start, stop - start).ToArray();
}
// Works similar to python's len function.
public static int len(List<int> list)
{
return list.Count;
}
public static int len(int[] list)
{
return list.Length;
}
// Works similar to python's range function.
public static int[] range(int start, int stop, int step)
{
int size = stop / step;
int[] arr = new int[size];
for(int i = 0; i < size; ++i)
arr[i] = start + (i * step);
return arr;
}
public static int[] range(int start, int stop)
{
return range(start, stop, 1);
}
}
}
现在,我认为您可能无法获得所需的结果,因为在 C#原始数据类型未通过引用传递。因此,解决这个问题的方法是将int
更改为一个类。这可能适合您:
public class INT
{
private int _i;
public INT() { _i = 0; }
public INT(int i) { _i = i; }
// Used to access the _i member.
public int self
{
get {return _i;}
set {_i = value;}
}
// Used to display what is stored inside.
public override string ToString()
{
return _i + "";
}
}
然后可以使用修改后的版本,以便将其更改为矩阵中的值。
using System;
using System.Collections.Generic;
namespace Rextester
{
public class Program
{
public class INT
{
private int _i;
public INT() { _i = 0; }
public INT(int i) { _i = i; }
// Used to access the _i member.
public int self
{
get {return _i;}
set {_i = value;}
}
// Used to display what is stored inside.
public override string ToString()
{
return _i + "";
}
}
public static void Main(string[] args)
{
List<INT> matrix = MakeList(9);
foreach(var element in Matrixify(3, matrix))
Console.WriteLine(ArrayToString(element));
}
// Used to make list easier...
public static List<INT> MakeList(int count)
{
List<INT> list = new List<INT>();
for(int i = 0; i < count; ++i)
list.Add(new INT());
return list;
}
// Used for testing display.
public static string ArrayToString(INT[] arr)
{
return "[" + string.Join<INT>(",", arr) + "]";
}
// Breaks a list into a matrix where delta is the # columns and # rows = len(list) / delta.
public static INT[][] Matrixify(int delta, List<INT> list)
{
// Get a range of start index points.
int[] arr = range(0, len(list), delta);
// Used to make the matrix.
List<INT[]> result = new List<INT[]>();
// Chops up into rows.
foreach(var i in arr)
result.Add(getRow(i, i + delta, list));
// Sends back as an array of arrays (aka matrix).
return result.ToArray();
}
// Grabs a row of the list passed in.
public static INT[] getRow(int start, int stop, List<INT> list)
{
return list.GetRange(start, stop - start).ToArray();
}
// Works similar to python's len function.
public static int len(List<INT> list)
{
return list.Count;
}
public static int len(List<int> list)
{
return list.Count;
}
public static int len(int[] list)
{
return list.Length;
}
// Works similar to python's range function.
public static int[] range(int start, int stop, int step)
{
int size = stop / step;
int[] arr = new int[size];
for(int i = 0; i < size; ++i)
arr[i] = start + (i * step);
return arr;
}
public static int[] range(int start, int stop)
{
return range(start, stop, 1);
}
}
}
希望这在某种程度上有所帮助。