使用坐标python 3打印完整的Matrix

时间:2018-02-13 04:38:45

标签: python python-3.x matrix printing

import random
grid = []
n=m=5
NG = [random.randint(0,n-1) for x in range(n)]
for i in range(5):
        grid.append((i,NG[i]))
Matrix = {(x,y):"-" for x in range(n) for y in range(m)}
point1 = (1, 2)
for i in range(len(grid)):
    Matrix[grid[i]] = "Q"
for i in range(0, len(grid)):
      for j in range(i, len(grid)):
            print(Matrix[(i,j)], end=' ')
      print()

输出

Q - - - - 
- - Q - 
- - - 
- Q 
- 

我需要填写所有内容,因此如果它是5x5矩阵,则所有25个空格都被占用。如果坐标在Matrix中,那么它将获得“Q”,否则它将获得“ - ”

2 个答案:

答案 0 :(得分:1)

问题在于你的j循环:

for j in range(i, len(grid)):

随着我的增加,每次都会减少一行。正如Lafexlos所建议的,您应该使用它来获得所需的输出:

range(len(grid))

将保持您的行大小。

答案 1 :(得分:0)

你也可以这样做:

    Dim Elems As HtmlElementCollection
    Elems = WebBrowser1.Document.GetElementsByTagName("button")

    If variavelinicial = 1 Then

        WebBrowser1.Document.GetElementsByTagName("input").GetElementsByName("username").Item(0).SetAttribute("value", "balhblah")
        WebBrowser1.Document.GetElementsByTagName("input").GetElementsByName("password").Item(0).SetAttribute("value", "blahbblah")
        WebBrowser1.Document.GetElementsByTagName("button")(0).InvokeMember ("click")

    End If

    On Error GoTo erro

    Dim data_hoje As Date = Today
    Dim data_hoje_formatada = data_hoje.ToString("yyyyMMdd")
    Dim fileReader As String = My.Computer.FileSystem.ReadAllText("C:\test.ics")
    Dim lineCount = File.ReadAllLines("C:\test.ics")
    Dim datas_end(lineCount.Length) As String
    Dim datas_start(lineCount.Length) As String

    If variavelinicial = 3 Then

        For i As Integer = 0 To lineCount.Length - 1

            If lineCount(i).Contains("DTEND") Then

                datas_end(x) = lineCount(i).Substring(lineCount(i).Length - 8)
                datas_start(x) = lineCount(i - 1).Substring(lineCount(i - 1).Length - 8)

                x = x + 1
                i = i + 2

                If data_hoje_formatada <= datas_start(x - 1) Then

                    WebBrowser1.Document.GetElementById("startDate").SetAttribute("value", datas_start(x - 1).Substring(4, 2) + "/" + datas_start(x - 1).Substring(6) + "/" + datas_start(x - 1).Substring(0, 4))
                    WebBrowser1.Document.GetElementById("endDate").SetAttribute("value", datas_end(x - 1).Substring(4, 2) + "/" + datas_end(x - 1).Substring(6) + "/" + datas_end(x - 1).Substring(0, 4))

                    WebBrowser1.Document.GetElementById("endDate").InvokeMember ("focus")
                    WebBrowser1.Document.GetElementById("endDate").InvokeMember ("click")

                    MsgBox ("added")

                    WebBrowser1.Document.GetElementById("addToCalendarButton").InvokeMember ("focus")
                    WebBrowser1.Document.GetElementById("addToCalendarButton").InvokeMember ("click")

                End If

            End If

        Next

    End If

erro:
    Err.Clear()
    Resume Next