Numpy and string formatting, use * to draw the shape of the field

Numpy’s character element matrix can be easily drawn; directly tearing Python strings by hand can also be completed easily.

(This note is suitable for coders who are familiar with loops and lists)


[The details of learning are a joyful process]

  • Python official website: python cutting edge. Unfortunately it’s the original English version. So, I want to practice English reading. “>https://www.python.org/

  • Free: Free for big names “Bible” tutorial “ python complete self-study tutorial” is not just as simple as the basics…
    Address: https://lqpybook.readthedocs.io/

Self-study is not a mysterious thing, a person The time spent studying by oneself in a lifetime is always longer than the time spent studying in school, and the time without a teacher is always longer than the time with a teacher.
Hua Luogeng

  • My CSDN homepage, My HOT blog, My Python learning personal memo
  • Highly recommended for good articles, Lao Qi Classroom


It is better to chase the wind than to wait for it...


numpy’s character element matrix, which can be easily drawn


Use * to draw the shape of the word “田”


(Direct python string tearing by hand can also be done easily)

Quality score of this article:


96
This article address:
https://blog.csdn.net/m0_57158496/article/details/134022375

CSDN quality score query entrance: http://www.csdn.net/qc


Directory

  • ◆?Use * to draw the shape of Tian character
    • 1. Topic description
    • 2. Algorithm analysis
      • 2.1 numpy string element matrix implementation
      • 2.2 implementation of hand-tearing python strings
      • 2.3 The simplest python code for tearing python strings by hand

◆?Use * to draw the shape of Tian character

1. Title description

  • Screenshot of question description


[The question comes from the CSDN Q&A community question “Use * to draw the shape of Tian”]


Back to table of contents

2. Algorithm analysis

Use * to draw the shape of a field, and both numpy and string formatting can do the job. Both methods complete the work by analyzing the shape characteristics of the drawn Tian characters, and then combining the characteristics of the tools used, design algorithms, and structural codes.

Here we focus on the implementation of string formatting; numpy implementation, after reading my previous study notes, you should be able to understand its code.

2.1 numpy string element matrix implementation

  • Can be completed using numpy two-dimensional array assignment
    My study notes have python source code:
    ?
    Numpy matrix frame(frame and inscribed rhombus in n×n matrix◇)
    ?
    Changing the middle of the graph into a cross becomes the solution to the problem of this article.
  • The shape of Tian when x = 2~9

    Only the code is posted here. To understand the code, please move to the numpy matrix frame of my study notes and click on the blue text to jump to read.

Python code for numpy implementation

#!/sur/bin/nve python
# coding: utf-8



def drawTian(n):
    import numpy as np
    a = np.full((n, n), ' ', dtype=str) # Create a character matrix.
    
    #drawing column #
    a[range(n), [0]*n] = '*'
    a[range(n), [n//2]*n] = '*'
    a[range(n), [-1]*n] = '*'
    
    #画行#
    a[[0]*n, range(n)] = '*'
    a[[n//2]*n, range(n)] = '*'
    a[[-1]*n, range(n)] = '*'
    
    print('\
'.join([" ".join(i) for i in a])) # String formatted output * field shape.


if __name__ == '__main__':

    for i in range(3, 10):
        print()
        drawTian(2*i-1)



Back to table of contents

2.2 implementation of tearing python strings by hand

  • Screenshot of code running effect

    ?
    Algorithm analysis:
    Carefully observe the shape of the word Tian, you can split it into 2x-1 lines, three of which have 2x-1 *(an English space between them), and the remaining lines have three * + spaces. According to this, we only need to output two types of strings, all* and three*. The three lines with all * are the first line, the last line and the middle line (the index subscript of the middle line is (2x-1)//2, the index subscript starts from zero, and the index of the middle line is exactly (2x-1)/ /2. For example, x = 3, (2x-1)//2 = 2; x = 3, its field shape and rows and columns are both 5*, and index 2 is the third row, which is the middle row) , once you understand this, it will not be difficult to read the code below.

Python code for tearing strings by hand

#!/sur/bin/nve python
# coding: utf-8


def drawTian(n):
    fullStars = '* '*(n)
    fullStars = ' '.join(list('*'*(n))) # Full * lines can also look like this.
    blank = ' '*(n-2)
    threeStars = '*' + blank + '*' + blank + '*'

    for i in range(n): # String formatting loop outputs *field shape line by line.

        if i in (0, n//2, n-1):
            print(fullStars)
        else:
            print(threeStars)


if __name__ == '__main__':

    for i in range(3, 10):
        print()
        drawTian(2*i-1)



Back to table of contents

2.3 The simplest python code for tearing python strings by hand

This if statement,

        if i in (0, n//2, n-1):
            print(fullStars)
        else:
            print(threeStars)

Can be written as a print() using a ternary operation statement

        print(fullStars if i in (0, n//2, n-1) else threeStars)

String hand-shred*field "simplest code"

#!/sur/bin/nve python
# coding: utf-8


def drawTian(n):
    fullStars = '* '*(n)
    fullStars = ' '.join(list('*'*(n))) # Full * lines can also look like this.
    blank = ' '*(n-2)
    threeStars = '*' + blank + '*' + blank + '*'

    for i in range(n): # String formatting loop outputs *field shape line by line.
        print(fullStars if i in (0, n//2, n-1) else threeStars)


if __name__ == '__main__':

    for i in range(3, 10):
        print()
        drawTian(2*i-1)


Back to top


Previous article:? Classic circular proposition: One hundred coins and one hundred chickens (One cub costs five coins, a female costs three coins, and three chicks cost one coin; One hundred coins and one hundred chickens (one hundred chickens and one hundred coins)
Next article:?

My HOT blog:

A total of 246 blog post note information were collected this time, with a total reading volume of 40.46w and an average reading volume of 1644. 16 blog post note index links with a reading volume of no less than 4,000 have been generated. Data collection was completed at 2023-10-12 05:41:03, taking 4 minutes and 41.10 seconds.

  1. First experience of ChatGPT domestic mirror site: chat, Python code generation, etc.
    ( 59262 reading)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/129035387
    Like: 126 Dislike: 0 Collection: 798 Reward: 0 Comment: 71
    This blog post was first published on 2023-02-14 23:46:33 and modified at the latest on 2023-07-03 05:50:55.
  2. The magic code that changes the nickname of QQ group
    ( 58086 reading)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/122566500
    Like: 24 Dislike: 0 Collection: 83 Reward: 0 Comment: 17
    This blog post was first published at 2022-01-18 19:15:08 and was modified at the latest at 2022-01-20 07:56:47.
  3. pandas data type DataFrame
    ( 9173 Reading)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/124525814
    Like: 6 Dislike: 0 Collection: 31 Reward: 0 Comment: 0
    This blog post was first published on 2022-05-01 13:20:17 and modified at the latest on 2022-05-08 08:46:13.
  4. Personal information extraction (string)
    ( 7215 Reading)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/124244618
    Like: 1 Dislike: 0 Collection: 13 Reward: 0 Comment: 0
    This blog post was first published on 2022-04-18 11:07:12 and modified at the latest on 2022-04-20 13:17:54.
  5. 7 ways to implement reverse order (descending order) of Python lists
    ( 7161 Reading)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/128271700
    Like: 5 Dislike: 0 Collection: 22 Reward: 0 Comment: 8
    This blog post was first published at 2022-12-11 23:54:15 and was modified at the latest at 2023-03-20 18:13:55.
  6. Roman Numeral Converter | Roman Numeral Generator
    ( 7035 Reading)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/122592047
    Like: 0 Dislike: 0 Collection: 1 Reward: 0 Comment: 0
    This blog post was first published at 2022-01-19 23:26:42 and was modified at the latest at 2022-01-21 18:37:46.
  7. Python string displayed in the center
    ( 6966 Reading)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/122163023
    Like: 1 Dislike: 0 Collection: 7 Reward: 0 Comment: 1
    Notes for this blog
  8. Recursive implementation and for implementation of Fibonacci sequence
    ( 5523 reads)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/122355295
    Like: 4 Dislike: 0 Collection: 2 Reward: 0 Comment: 8
    Notes for this blog
  9. python clear screen
    ( 5108 reads)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/120762101
    Like: 0 Dislike: 0 Collection: 8 Reward: 0 Comment: 0
    Notes for this blog
  10. Exercise: String statistics (pit: fstring error report)
    ( 5103 reads)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/121723096
    Like: 0 Dislike: 0 Collection: 1 Reward: 0 Comment: 0
    Notes for this blog
  11. Carriage return, line feed, and carriage return and line feed
    ( 5093 reads)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/123109488
    Like: 1 Dislike: 0 Collection: 2 Reward: 0 Comment: 0
    This blog post was first published at 2022-02-24 13:10:02 and modified at the latest at 2022-02-25 20:07:40.
  12. Exercise: Nim game (smart version/fool style? Man-machine battle)
    ( 4943 reads)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/121645399
    Like: 14 Dislike: 0 Collection: 42 Reward: 0 Comment: 0
    Notes for this blog
  13. Password strength detector
    ( 4323 reads)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/121739694
    Like: 1 Dislike: 0 Collection: 4 Reward: 0 Comment: 0
    This blog post was first published at 2021-12-06 09:08:25 and modified at the latest at 2022-11-27 09:39:39.
  14. Exercise: Generate 100 random positive integers
    ( 4274 reads)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/122558220
    Like: 1 Dislike: 0 Collection: 6 Reward: 0 Comment: 0
    This blog post was first published at 2022-01-18 13:31:36 and was modified at the latest at 2022-01-20 07:58:12.
  15. My Python.color() (Python color printing control)
    ( 4159 reads)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/123194259
    Like: 2 Dislike: 0 Collection: 8 Reward: 0 Comment: 0
    This blog post was first published on 2022-02-28 22:46:21 and modified at the latest on 2022-03-03 10:30:03.
  16. Roman numeral converter (implemented using the modulus of the value of the Roman numeral construction element)
    ( 4149 reads)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/122608526
    Like: 0 Dislike: 0 Collection: 0 Reward: 0 Comment: 0
    This blog post was first published at 2022-01-20 19:38:12 and modified at the latest at 2022-01-21 18:32:02.


Recommendation conditions
Reads exceeded 3,000


(For more hot blogs, please click on the blue text to jump to read)


Back to top



Lao Qi comic avatar

Excellent articles:

  • Recommended articles: Qi Wei Shu Manuscript “Python Complete Self-Study Tutorial” FreeSerial(has been completed and compiled into a book, and there is also a PDF version for permanent sharing on Baidu Netdisk, Click Jump for free download.)
  • Three major features of OPP: property in encapsulation
  • Understanding python’ through built-in objects
  • regular expression
  • The role of “*” in python
  • Python complete self-study manual
  • walrus operator
  • `!=` in Python is different from `is not`
  • The right way to learn programming

Source: Lao Qi Classroom


Back to top

◆ Python Getting Started Guide[Python 3.6.3]

Highly recommended for good articles:

  • A high-quality creator in the full-stack field – [Hanlao](still a student in a domestic university) blog post “Non-technical article – about English and how to speak correctly “Asking questions”, “English” and “Being able to ask questions” are two powerful tools for learning programming.
  • [Applicable fields of 8 major programming languages] Don’t rush to choose linguistic programming first, first look at what they can do
  • Good habits of reliable programmers
  • The three major elements of “Function function, end condition, and function equivalence” in the high-quality and good article by the boss Shuaidi will help you understand recursion.

CSDN Practical Tips Blog:

  • 8 Python practical tips that are extremely useful
  • python ignore warning
  • Python code writing specifications
  • Python’s docstring specification (standard way of writing documentation)