How to print characters as a table with Python's print function

How to print characters as a table with Python's print function

Introduction

Given the numbers (NM) that you often see in problem books, A solution when the output result is obtained by some character string of N rows x M columns.

Example) Star chart, multiplication table, etc.

What you want

& + + + +
+ & + + +
+ + & + +
+ + + & +
+ + + + &

input

N M

Code and commentary

import numpy as np

#(N M)= (5 5)Suppose there is an input in the form of
n, m = input().split()
n, m = int(n), int(m)

#Create a two-dimensional array with N rows and M columns(This time, all 0 arrays)
hyou = np.zeros((n,m))

#If there are multiple character strings to be displayed on the star chart, etc., set "Numbers in the array of that part" to 1.,2,3...And replace it in different cases
#This time it's a simple one that just puts 1 when the numbers in the row and column are the same

for i in range(n):
   
    for j in range(m):
        
        if i == j:
            hyou[i][j] = 1


#Numerical value indicating the element at the end of the line(Subscript of the last element of a column in a two-dimensional array)
x =  m -1
             
for i in range(n):
 
#Whether or not the output line ends is determined by whether j == x
#End if not the end=" "Output blank space with, and at the end, start a new line with the print function as it is
#Change the characters to be output for each number
   
    for j in range(m):
        
        if hyou[i][j] == 0 and j != x:
            print("+", end = " ")
        
        elif hyou[i][j] == 1 and j != x:
            print("&", end = " ")
            
        if hyou[i][j] == 0 and j == x:
            print("+")
        
        elif hyou[i][j] == 1 and j == x:
            print("&" )           

Finally

It's simple, but I think it's easy to forget, so I'll make a note of it.

Recommended Posts

How to print characters as a table with Python's print function
How to call a function
How to display DataFrame as a table in Markdown
How to make a recursive function
[Introduction to Python] How to split a character string with the split function
[Python 3.8 ~] How to define a recursive function smartly with a lambda expression
How to add a package with PyCharm
[Python] Explains how to use the range function with a concrete example
A function that generates a list with up to 3 prime numbers as elements
[Introduction to Python] How to write a character string with the format function
How to read a CSV file with Python 2/3
How to send a message to LINE with curl
How to draw a 2-axis graph with pyplot
How to create a function object from a string
How to make a dictionary with a hierarchical structure.
How to read an array with Python's ConfigParser
How to create a multi-platform app with kivy
[Go] How to write or call a function
[Python] How to handle Japanese characters with openCV
How to Mock a Public function in Pytest
How to convert / restore a string with [] in python
I tried to create a table only with Django
[Python] How to draw a line graph with Matplotlib
[Introduction to Python] How to iterate with the range function?
How to create a submenu with the [Blender] plugin
Try to dynamically create a Checkbutton with Python's Tkinter
How to get a logged-in user with Django's forms.py
How to make a shooting game with toio (Part 1)
Make a function to describe Japanese fonts with OpenCV
How to save a table scraped by python to csv
[Python] How to rewrite the table style with python-pptx [python-pptx]
[Python] How to create a 2D histogram with Matplotlib
[Python] How to call a c function from python (ctypes)
[Python] How to draw a scatter plot with Matplotlib
How to identify the element with the smallest number of characters in a Python list?
How to use python multiprocessing (continued 3) apply_async in class with Pool as a member
How to deploy a web app made with Flask to Heroku
How to use Fujifilm X-T3 as a webcam on Ubuntu 20.04
How to convert an array to a dictionary with Python [Application]
How to import NoteBook as a module in Jupyter (IPython)
How to output a document in pdf format with Sphinx
How to create a flow mesh around a cylinder with snappyHexMesh
How to update with SQLAlchemy?
How to make a simple Flappy Bird game with pygame
How to use cuML SVC as a Gridsearch CV classifier
How to cast with Theano
How to hack a terminal
[Linux] How to deal with garbled characters when viewing files
How to display a list of installable versions with pyenv
How to extract other than a specific index with Numpy
How to register a package on PyPI (as of September 2017)
A story about how to deal with the CORS problem
How to build a python2.7 series development environment with Vagrant
How to Alter with SQLAlchemy?
How to separate strings with','
How to use a file other than .fabricrc as a configuration file
How to RDP with Fedora31
[Python] How to output a pandas table to an excel file
How to import NoteBook as a module in Jupyter (IPython)
How to print characters to the console before booting on ARM
How to Delete with SQLAlchemy?