in view ofPrevious article.The last three questions in the
1. Can the above program be optimized (e.g. functionally identical)
2、Creating three 3 instances, using 3 statements, can you build a function to automatically create n instances by entering only a number n? At the same time, the num_times of each instance is randomized, (when n is larger, num_times should be smaller)
3, when the realization of the above function, the program runs, only enter a parameter (the number of instances created), it will automatically generate the corresponding num_times, and call the relevant functions to generate the corresponding chart.
As you can, a multiplication formula is used in the class Rand_moving() to calculate the direction and moving position of each step of the
x_direction = choice([1,-1]) #x moves in a direction where 1 goes up, 0 stays the same, -1 goes down x_distance = choice([0,1,2,3,4,5]) #pixels per shift of x. x_step = x_direction*x_distance #Direction of travel multiplied by distance traveled to determine distance traveled along x y_direction = choice([1,-1]) The direction of movement of #y, 1 is up, 0 is constant, -1 is down y_distance = choice([0,1,2,3,4,5]) #y of pixels per shift. y_step = y_direction*y_distance #Direction of movement multiplied by distance traveled,in order to determine the distance along theyDistance moved
So it is possible to organize a calculation that can be called directly, and as for 2, 3 it is obvious that it can be done as well.
The first step is to present the multiplication formula as a separate method acting on itself, with the following code:
def get_step(self,direction,distance): return distance*direction def fill_moving(self): while len(self.x_values)<self.num_times:# The loop keeps running until the walk contains the desired number of points num_times x_step = self.get_step(choice([1,-1]),choice([0,1,2,3,4,5])) # Call the get_step method directly to make the code more concise y_step = self.get_step(choice([1,-1]),choice([0,1,2,3,4,5]))
Complete Q1 and the code becomes more concise and clear.
Questions 2 and 3:
1) To be able to generate the number of instances n, and there is a corresponding number of moves num_times, you can consider using a dictionary, i.e., the number of instances for the key, the corresponding number of moves num_times for the key value, when the input is 2, such as caselist={'1':'150000', '2':'250000'}, '2':'250000'}, '2':'250000'} and '2':'250000'}, you can use the dictionary as the key. '2':'250,000'}
2) Read each item of the dictionary and pass the corresponding key and value to the instance created by the Rand_moving class, the number of key is the number of instances that need to be created, and the corresponding value value is the number of moves.
3) Enter the number, which is the number of dictionary keys, into the dictionary, while using a random function to generate a num_times saved to the corresponding value.
Finishing thoughts:
1, redefine a class New_case() Function: receive a data and automatically generate a dictionary based on this data.
class New_case(): #Define the New_case class def __init__(self,numbers): # Define the number of instances to be created =numbers ={} # Define an empty caselist dictionary = 0 while < : # When the variable case is less than a given value, the += 1 times = choice([100000,150000,200000,250000])# Randomly select a number of moves [] = times #commander-in-chief (military)valuetogether withkeyhomologous
2, you need to cycle through the dictionary key and value, and pass the value to the class Rand_moving, and then run fill_moving () to generate data and save it to the list, and then use () for drawing
for key,value in (): # Dictionary not empty colorkey=str(key) # Convert dictionary keywords to strings and store them in the variable colorkey examplecase = Rand_moving(int(value)) #Create instances, pass corresponding value values to class Rand_moving examplecase.fill_moving() # Call the method fill_moving() in class Rand_moving to calculate the moving-related data and save it to the list (dpi=128,figsize=(12, 10)) #Create Screen Screen (examplecase.x_values,examplecase.y_values,c=[colorkey],s=15) ()
The reason why the code c=y_values, cmap= in the previous post is no longer used here is because red Reds keep appearing when looping here, and for comparison, a new dictionary colors{} was created to map the number of generators to the color. So the above code is modified to c=[colorkey]
The entire code of class New_case() is given below:
import as plt from rand_moving import * class New_case(): #Define the New_case class def __init__(self,numbers): # Define the number of instances to be created =numbers ={} # Define an empty list of cases = 0 # Define a case variable ={'1':'red','2':'orange','3':'yellow','4':'green','5':'blue','6':'puple'}# creates a new dictionary, colors{}, which maps the number of generated colors to the number of colors. while < : # Less than a given number of instances += 1 times = choice([100000,150000,200000,250000]) # Generate a random number of moves [] = times # Save the variable case as key and times as value in a dictionary. def case_moving(self): # Redefine a method that accesses all entries of the dictionary for key,value in (): # Dictionary not empty colorkey=str(key) # Convert dictionary keywords to strings and store them in the variable colorkey examplecase = Rand_moving(int(value)) #Create instances, pass corresponding value values to class Rand_moving examplecase.fill_moving() # Call the method fill_moving() in class Rand_moving to calculate the moving-related data and save it to the list (dpi=128,figsize=(12, 10)) #Create Screen Screen (examplecase.x_values,examplecase.y_values,c=[colorkey],s=15)# Note the new dictionary colors that call the above ()
3. Main program
There's an interaction in the main program that requires a data input and then calls the relevant related class to create an instance (slowly becoming a packet tweaker.)! ^v^)
import as plt from rand_moving import * from new_case import * print("Please enter the number:") #Interaction, please enter a number and run the simulation, it doesn't take much data. n = input() # Save the input into the variable n. Note that all inputs are strings. testcase = New_case(int(n)) # of instances created by converting n to integer data testcase.case_moving()
The actual running effect, input 4, to generate 4 data graphs (the original graph has been reduced in order to show a fuller picture):
= {'1':'red','2':'orange','3':'yellow','4':'green','5':'blue','6':'puple'} Note: The colors of the graphs correspond to those in the COLORS dictionary, respectively.
Of course, if you think that the axis is an eyesore, then in the class class New_case(), after the () add
().get_xaxis().set_visible(False) ().get_yaxis().set_visible(False)
to this article on the use of Python random function to generate changes in the graphic details of the article is introduced to this, more related Python random function to generate changes in the graphic content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!