map function-Basic Python grammar learned with an interesting sample program

Background

I didn't feel like memorizing the basic grammar without an interesting sample program, so I decided to create a sample program for each basic grammar.

By the way, the definition of interesting is difficult, It's just interesting on my scale, so I'll make a funny sample program (maybe a little surreal), and there may be calculations like this one.

As a premise, I am a super beginner. I'm learning Python for the server side of web applications, and I'll be learning Django after finishing the basic grammar.

◆ Map Practice

Create a program that calculates the number of divisors for integers from 1 to 100. Create two patterns, one is to calculate while extracting the elements of the list with for, and the other is to calculate with map.

Use the list

x = range(1,100)
y = []
cnt = 0
for i in x:
    cnt = 0
    j = 1
    #Divide by the number less than or equal to x
    while j <= i:
        #If it is divisible, it is a divisor, so count up
        if x[i-1] % j == 0 :
            cnt += 1
        j += 1
    y.append(cnt)
print(x)
print(y)

Use map

def yakusucalc(x1):
    cnt = 0
    j = 1
 Try dividing by a number less than # x1
    while j <= x1:
 #If it is divisible, it is a divisor, so count up
        if x1 % j == 0 :
            cnt += 1
        j += 1
    return cnt
x = range(1,100)
y = list(map(yakusucalc, x))
print(x)
print(y)

By the way, when the graph is displayed, it looks like this. graph.png

The code to display the graph is as follows.

# This line is required when running on Jupyter Notebook
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import math

x = range(1,100)
# Calculate the number of divisors.
def yakusucalc(x1):
    cnt = 0
    j = 1
 Try dividing by a number less than # x1
    while j <= x1:
 #If it is divisible, it is a divisor, so count up
        if x1 % j == 0 :
            cnt += 1
        j += 1
    return cnt

y = list(map(yakusucalc, x))
print(x)
print(y)

plt.plot(x, y)
plt.show

By using map, the code has been simplified.

Postscript (2020/1/26) yakusucalc can be written in one line as follows. (Thank you for your comment)

def yakusucalc(value):
    return sum(value % yakusu == 0 for yakusu in range(1, value + 1))

Recommended Posts

map function-Basic Python grammar learned with an interesting sample program
Sample program that outputs syslog with Python logging
Sample data created with python
Creating an egg with python
Get started with Python! ~ ② Grammar ~
[Python] Sample code for Python grammar
I learned Python basic grammar
Cut out an image with python
[Python] Object-oriented programming learned with Pokemon
Debug python multiprocess program with VSCode
Create an Excel file with Python3
I sent an SMS with Python
Perceptron learning experiment learned with Python
Python data structures learned with chemoinformatics
Efficient net pick-up learned with Python
1. Statistics learned with Python 1-1. Basic statistics (Pandas)
Draw an illustration with Python + OpenCV
[Python] Send an email with outlook
[Python] Calendar-style heat map (with holiday display)
[Python] Building an environment with Anaconda [Mac]
[Python] A program that creates stairs with #
Algorithm learned with Python 9th: Linear search
Algorithm learned with Python 7th: Year conversion
Note when creating an environment with python
Algorithm learned with Python 8th: Evaluation of algorithm
2D FEM stress analysis program with Python
Quickly create an excel file with Python #python
Algorithm learned with Python 4th: Prime numbers
Map rent information on a map with python
I tried sending an email with python.
HTML document your Python program with Sphinx
Algorithm learned with Python 2nd: Vending machine
Algorithm learned with Python 19th: Sorting (heapsort)
Algorithm learned with Python 6th: Leap year
1. Statistics learned with Python 1-3. Calculation of various statistics (statistics)
[Python] Quickly create an API with Flask
Scraping from an authenticated site with python
I made a Hex map with Python
Sample program to display video with PyQt
Create an English word app with python
Send an email with Amazon SES + Python
Join an online judge with Python 3.x
Try drawing a map with python + cartopy 0.18.0
Algorithm learned with Python 3rd: Radix conversion
Let's develop an investment algorithm with Python 1
Algorithm learned with Python 12th: Maze search
Algorithm learned with Python 11th: Tree structure
Sample to convert image to Wavelet with Python
Create an animated time series map of coronavirus infection status with python + plotly