網(wǎng)站首頁 編程語言 正文
鑒于上一篇中最后三個問題:
1、上述程序是否能進行優(yōu)化(比如功能相同的)
2、創(chuàng)建三個3個實例,用了3個語句,能否建一個函數(shù),只輸入一個數(shù)n,就自動創(chuàng)建n個實例?同時,每個實例的num_times隨機,(n比較大時,num_times應該比較小)
3、當實現(xiàn)上述功能后,程序運行,只輸入一個參數(shù)(創(chuàng)建實例的個數(shù)),就會自動生成對應的num_times,并分別調(diào)用相關(guān)函數(shù)生成對應圖表。
可以,在類Rand_moving()中計算每一步的方向和移動位置時,都用到了一個乘法公式,
x_direction = choice([1,-1]) #x的移動方向,1向上,0不變,-1向下
x_distance = choice([0,1,2,3,4,5]) #x的每次移動的像素,
x_step = x_direction*x_distance #移動方向乘以移動距離,以確定沿x移動的距離
y_direction = choice([1,-1]) #y的移動方向,1向上,0不變,-1向下
y_distance = choice([0,1,2,3,4,5]) #y的每次移動的像素,
y_step = y_direction*y_distance #移動方向乘以移動距離,以確定沿y移動的距離
因此可以整理出一個計算方法,可以直接調(diào)用,至于2,3很明顯,也是可以完成的。
第一步,將乘法公式提出來單獨形成一個作用于自身的方法,代碼如下:
def get_step(self,direction,distance):
return distance*direction
def fill_moving(self):
while len(self.x_values)<self.num_times:#循環(huán)不斷運行,直到漫步包含所需數(shù)量的點num_times
x_step = self.get_step(choice([1,-1]),choice([0,1,2,3,4,5])) #直接調(diào)用get_step方法,使代碼更加簡潔
y_step = self.get_step(choice([1,-1]),choice([0,1,2,3,4,5]))
完成第1問,代碼變得更加簡潔明了。
第2、3個問題:
1)要能生成實例個數(shù)n,且還有對應的移動次數(shù)num_times,可考慮用字典,即實例個數(shù)為key,對應移動次數(shù)num_times為鍵值,當輸入2時,如? caselist={’1‘:'150000',’2‘:'250000'}
2)讀取字典每個項目,將對應的key和value傳遞給由Rand_moving類創(chuàng)建的實例,key的個數(shù)即為需要創(chuàng)建實例的個數(shù),對應的value值為移動次數(shù)。
3)輸入數(shù)字,即為字典key的個數(shù),存入字典,同時利用隨機函數(shù)生成一個num_times保存到對應的value中。
完成思路:
1,重新定義一個類 New_case() 作用:接收一個數(shù)據(jù),并根據(jù)這個數(shù)據(jù)自動生成一個字典,
class New_case():
#定義New_case類
def __init__(self,numbers): #定義要創(chuàng)建的實例個數(shù)
self.numbers=numbers
self.caselist={} #定義一個空的caselist字典
self.case = 0
while self.case < self.numbers: #當變量case小于給定值時,
self.case += 1
times = choice([100000,150000,200000,250000])#隨機選擇一個移動次數(shù)
self.caselist[self.case] = times #將value與key對應
2、需要循環(huán)讀取字典的key和value,并將value傳遞給類Rand_moving,隨后再運行fill_moving()生成數(shù)據(jù)并保存到列表,隨即用plt.scatter()進行繪圖
for key,value in self.caselist.items(): #字典不為空
colorkey=str(key) # 將字典關(guān)鍵字轉(zhuǎn)為字符串存到變量colorkey中
examplecase = Rand_moving(int(value)) #創(chuàng)建實例,將對應的value值傳遞類Rand_moving
examplecase.fill_moving() #調(diào)用類Rand_moving中的方法fill_moving()計算移動相關(guān)數(shù)據(jù)并保存到列表中
plt.figure(dpi=128,figsize=(12, 10)) #創(chuàng)建畫面屏幕
plt.scatter(examplecase.x_values,examplecase.y_values,c=self.colors[colorkey],s=15)
plt.show()
上篇中的代碼 ?c=y_values, cmap=plt.cm.Reds為什么這里不再用,是因為這里循環(huán)的時候一直出現(xiàn)紅色Reds,為了對比,創(chuàng)建了一新字典colors{},將生成的個數(shù)與顏色相對應。所以上述代碼中修改為 c=self.colors[colorkey]
類New_case()?全部代碼如下:
import matplotlib.pyplot as plt
from rand_moving import *
class New_case():
#定義New_case類
def __init__(self,numbers): #定義要創(chuàng)建的實例個數(shù)
self.numbers=numbers
self.caselist={} #定義一個空的cases列表
self.case = 0 #定義一個case變量
self.colors={'1':'red','2':'orange','3':'yellow','4':'green','5':'blue','6':'puple'}#創(chuàng)建了一新字典colors{},將生成的個數(shù)與顏色相對應
while self.case < self.numbers: #小于給定實例個數(shù)時
self.case += 1
times = choice([100000,150000,200000,250000]) #隨機生成一個移動次數(shù)
self.caselist[self.case] = times #將變量case作為key, times作為value保存到字典中
def case_moving(self): #重新定義一個方法,即訪問字典所有項
for key,value in self.caselist.items(): #字典不為空
colorkey=str(key) # 將字典關(guān)鍵字轉(zhuǎn)為字符串存到變量colorkey中
examplecase = Rand_moving(int(value)) #創(chuàng)建實例,將對應的value值傳遞類Rand_moving
examplecase.fill_moving() #調(diào)用類Rand_moving中的方法fill_moving()計算移動相關(guān)數(shù)據(jù)并保存到列表中
plt.figure(dpi=128,figsize=(12, 10)) #創(chuàng)建畫面屏幕
plt.scatter(examplecase.x_values,examplecase.y_values,c=self.colors[colorkey],s=15)#注意調(diào)用了上述新字典的顏色
plt.show()
3、主程序
主程序中有一個交互,需要輸入一個數(shù)據(jù),然后調(diào)用相關(guān)相關(guān)類創(chuàng)建實例(慢慢成調(diào)包俠了!^v^)
import matplotlib.pyplot as plt
from rand_moving import *
from new_case import *
print("Please enter the number:") #交互,請輸入一個數(shù),模擬運行,不需要太大的數(shù)據(jù)。
n = input() #將輸入的數(shù)據(jù)保存到變量n中,注意所有輸入均為字符串,
testcase = New_case(int(n)) #將n轉(zhuǎn)為整型數(shù)據(jù),創(chuàng)建實例個數(shù)
testcase.case_moving()
實際運行效果,輸入4,生成4個數(shù)據(jù)圖形(為展示較全,原圖已縮小):
self.colors={'1':'red','2':'orange','3':'yellow','4':'green','5':'blue','6':'puple'}?注意:圖的顏色分別與colors字典中對應。
當然如果覺得數(shù)軸很礙眼,那就在類類New_case()?中的plt.figure()之后加上
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
原文鏈接:https://www.cnblogs.com/codingchen/p/16192714.html
相關(guān)推薦
- 2023-04-24 Numpy創(chuàng)建NumPy矩陣的簡單實現(xiàn)_python
- 2022-01-08 iframe 監(jiān)聽滾動事件并滾動到指定位置
- 2022-09-30 python實現(xiàn)圖像邊緣檢測_python
- 2022-12-21 Redis數(shù)據(jù)庫原理深入刨析_Redis
- 2022-09-18 Go語言單控制器和多控制器使用詳解_Golang
- 2022-07-19 Git Clone命令直接使用用戶名密碼Clone
- 2022-10-05 WPF實現(xiàn)好看的Loading動畫的示例代碼_C#教程
- 2023-01-05 使用sqlplus連接Oracle數(shù)據(jù)庫問題_oracle
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支