[Algorithm x Python] Calculation of basic statistics (total value, maximum value, minimum value)

I will write about algorithms and Python. This time, I will write not only how to calculate a simple calculation using a function, but also how to calculate it when a function is not used.

table of contents

  1. Confirmation of arithmetic operators and comparison operators
  2. List comprehension
  3. Find the total value
  4. Find the maximum value
  5. Find the minimum value Finally

0. Confirmation of arithmetic operators and comparison operators

Arithmetic operator

◯ About symbols for performing calculations.

operator meaning Example result
+ addition 3 + 4 7
- subtraction 10 - 2 8
* Multiplication 5 * -6 -30
/ Floating point division 5 / 2 2.5
// Integer division 5 // 2 2
% Too much division 7 % 2 1
** ~Ride(index) 2 ** 4 16

Comparison operator

◯ About symbols for comparison. Returns a Boolean value of True or False.

meaning operator
equal ==
Not equal !=
Smaller <
Less than <=
Greater >
Not equal >=
Is an element in ~

List comprehension

◯ List comprehensions are used to efficiently create various types of lists. I would like to use various lists in this article as well, so I will write it in advance.

What is list comprehension?

This is one way to make a list. It is defined in the form of ** [element for element to store in list] **. The ** element ** stored in the list is a ** expression ** that uses the element. A ** iterable object ** is an object that can retrieve elements one by one.

Example of using list comprehension notation

◯ The range () function returns an iterable object.

ex0.py


#Takes integers between 1 and 10 one by one and stores their values in the numbers list
numbers = [number for number in range(1,10)]
print(numbers)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

ex1.py


#Extract integers from 1 to less than 10 one by one and store the value multiplied by 2 in the numbers list.
numbers = [number * 2 for number in range(1,10)]
print(numbers)
[2, 4, 6, 8, 10, 12, 14, 16, 18]

ex2.py


#Extract integers from 1 to less than 10 one by one and store only even values in the numbers list.
numbers = [number for number in range(1,10) if number % 2 == 0]
print(numbers)
[2, 4, 6, 8]

ex3.py


#List of strings
list_1 = ['1','2','3']
#List of strings list_Extract elements from 1 one by one, convert them to integers, and store them in the list numbers
numbers = [int(i) for i in list_1]
print(numbers)
[1, 2, 3]

ex4.py


#If the extracted element is odd, store it as it is. If it is even, square it and store it.
numbers = [i if i % 2 == 1 else i**2 for i in range(10)]
print(numbers)
[0, 1, 4, 3, 16, 5, 36, 7, 64, 9]

2. Find the total value

How to find the total value using the sum () function

◯ You can easily find the total value of the elements in the list by using the sum () function.

#Prepare a list to use
numbers = [1,2,3,4,5]

#sum(list)Use like that
print(sum(numbers))
15

How to find the total value using an iterator

numbers = [1,2,3,4,5,6]

#Variable sum representing the sum value_of_Define numbers
#Nothing has been added yet, so set it to 0
sum_of_numbers = 0

#Use the for loop to get the elements of the list one by one
#range()Is'Between'In that sense, it indicates the range of iteration
#len(list)でlistの要素数(length)To get
for i in range(len(numbers)):
    #Sum the retrieved elements one by one_of_Add to numbers
    sum_of_numbers += numbers[i]

print('sum of numbers = ', sum_of_numbers)
sum of numbers =  21

◯ ** Point **: Iterator It retrieves and returns one element from a list, dictionary, etc. for each iteration. Lists, dictionaries, etc. are iterable objects that support iterators. Other iterable objects include strings, tuples, and sets.

Point: **+=, -=, *=, /=, //=, %=, = It is an abbreviation for the calculation process of substituting the calculated value. (Example)

a = 3

#a = a * 5
a *= 5
print(a)
15

3. Find the maximum value

How to find the maximum value using the max () function

◯ Used in the form of max (list) Convert the string to an integer, store it in a new list, and try to find the maximum value of the elements in it.

list_1 = ['1','2','3']

#list_1 element int()Use a function to convert it to an integer and then store it in the numbers list
numbers = [int(i) for i in list_1]

print('numbers = ',numbers)
#Max the maximum value in the list(list)Ask in the form of
print('max of numbers = ',max(numbers))
numbers =  [1, 2, 3]
max of numbers =  3

How to find the maximum value using comparison

◯ This is a method of comparing the elements with the provisional maximum value.


#Try defining list elements in list comprehension notation
#Store elements in the list that satisfy 1 or more and less than 11 and the remainder divided by 2 satisfies 0.
#The list is[2, 4, 6, 8, 10]Becomes
numbers = [number for number in range(1,11) if number % 2 == 0]

#First element(This time 2)To the temporary maximum value
max_of_numbers = numbers[0]

#Extract the elements of the list one by one.
#Exclude the first element because it is not a comparison target
for i in range(1,len(numbers)):
    #If the compared factors are greater than the current maximum
    if(max_of_numbers < numbers[i]):
        #Update maximum
        max_of_numbers = numbers[i]

print('numbers = ',numbers)
print('max of numbers = ', max_of_numbers)
numbers =  [2, 4, 6, 8, 10]
max of numbers =  10

4. Find the minimum value

How to find the minimum value using the min () function

◯ Used in the form of min (list)

#Even-numbered values in the range 0 to less than 10 are squared before being stored in the list.
#Store odd values as they are
#The list is[0, 1, 4, 3, 16, 5, 36, 7, 64, 9]Becomes
numbers = [i if i % 2 == 1 else i**2 for i in range(10)]

print('numbers = ',numbers)
#The minimum value in the list is min(list)Ask in the form of
print('min of numbers = ',min(numbers))
numbers = [0, 1, 4, 3, 16, 5, 36, 7, 64, 9]
min of numbers =  0

Point: if-else- ** If-, otherwise-** means. A comparison statement that determines if the condition is met. In this case, If the element is odd (if i% 2 == 1)-if not, square the element (if even)


How to find the minimum value using comparison

◯ This is a method of comparing the elements with the provisional minimum value. This time, I will make a list of random integers and select the minimum value from them.

#Import random module
import random
#Create a list of integers from 0 to 100 numbers
numbers = [number for number in range(0,101)]
#Randomly pick 10 elements from the numbers list and list them
#Find the minimum value of the elements in this list
random_numbers = random.choices(numbers,k = 10)

#First element(This time 2)To a temporary minimum value
min_of_random_numbers = random_numbers[0]

#Extract the elements of the list one by one.
#Exclude the first element because it is not a comparison target
for i in range(1,len(random_numbers)):
    #If the compared factors are less than the current minimum
    if(min_of_random_numbers > random_numbers[i]):
        #Update the minimum value
        min_of_random_numbers = random_numbers[i]

print('random_numbers = ',random_numbers)
print('min of random_numbers = ', min_of_random_numbers)
random_numbers =  [33, 99, 33, 27, 25, 42, 19, 37, 14, 15]
min of random_numbers =  14

◯ ** Point **: random module This time, we will use the choices (references, number of elements) function in the random module. You can use the choices () function to create a list of the number of elements specified by k =. Also, the elements of the list to be created will be randomly selected elements from the ones to be referenced (in this case, the numbers list).

Reference article: choice, sample, choices to randomly select elements from a list in Python

Finally

Thank you for reading. Next time, I would like to write the calculation of basic statistics Part2 (mean, median, mode). I would be grateful if you could point out any mistakes or improvements.

Recommended Posts

[Algorithm x Python] Calculation of basic statistics (total value, maximum value, minimum value)
[Algorithm x Python] Calculation of basic statistics Part2 (mean, median, mode)
[Algorithm x Python] Calculation of basic statistics Part3 (range, variance, standard deviation, coefficient of variation)
1. Statistics learned with Python 1-3. Calculation of various statistics (statistics)
[Python] How to use list 2 Reference of list value, number of elements, maximum value, minimum value
1. Statistics learned with Python 1-2. Calculation of various statistics (Numpy)
Sequential calculation of mean value with online algorithm
Python basic grammar / algorithm
Basic knowledge of Python
[Python] Heron's formula functionalization and calculation of the maximum area
Find the index of the maximum value (minimum value) of a multidimensional array
[Scientific / technical calculation by Python] Basic operation of arrays, numpy
[python] Value of function object (?)
Shapley value calculation in Python
2.x, 3.x character code of python
Basic usage of Python f-string
[Fundamental Information Technology Engineer Examination] I wrote an algorithm for the maximum value of an array in Python.
Basics of Python x GIS (Part 3)
Basics of Python x GIS (Part 2)
Basic grammar of Python3 system (dictionary)
Implementation of Dijkstra's algorithm with python
[Python] Calculation of Kappa (k) coefficient
Basic study of OpenCV with Python
I measured 6 methods to get the index of the maximum value (minimum value) of the list