将列表打印为矩阵

本文关键字:打印 列表 | 更新日期: 2023-09-27 18:21:58

我想做这样的事情,但在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算法。

将列表打印为矩阵

你可以用一种简单的方法

public static void Print(int[][] matrix)
{
    foreach (var row in matrix)
        Console.WriteLine($"[{string.Join(",", row)}]");
}

另一个版本,如果你有一个长度为9的列表(可能是井字数据)

public static void PrintMatrix(List<int> tictactoe)
{
    for (int i = 0; i < 3; i = i + 3)
        Console.WriteLine($"[{tictactoe[i]},{tictactoe[i + 1]},{tictactoe[i + 2]}]");
}

我试图使一些函数看起来类似于python。还有一些东西可以做,使它看起来更相似,但从我目前所看到的来看,这将涉及一些lambda函数,这可能有点过头了。如果需要,可以在这里测试运行。

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

希望这在一定程度上有所帮助。