gpt4 book ai didi

python - 有没有办法减少类中大量重复的代码

转载 作者:行者123 更新时间:2023-12-01 06:42:13 25 4
gpt4 key购买 nike

我有以下代码:

import matplotlib.pyplot as plt ### imports plotting
import numpy as np
import pandas as pd

class Population:
def __init__(self,name,population):
self.name = name
self.population = population
self.conservative = self.population * 48/100
self.labour = self.population * 30/100
self.libDem = self.population * 12/100
self.green = self.population * 5/100
self.brexit = self.population * 3/100
self.others = self.population * 2/100

def distribution(self):
print(self.conservative)
print(self.labour)
print(self.libDem)
print(self.brexit)
print(self.others)

def rulingParty(self):
parties = {"Conservative":self.conservative,"Labour":self.labour,"LibDem":self.libDem,"Green":self.green,"Brexit":self.brexit,"Other":self.others}
return max(parties, key=parties.get)

def addCon(self,amount, Lab=1/5,LD=1/5,GRN=1/5,BXT=1/5,Others=1/5):
self.conservative += amount
self.labour += -amount * Lab
self.libDem += -amount * LD
self.green += -amount * GRN
self.brexit += -amount * BXT
self.others += -amount * Others

def addLab(self,amount, Con=1/5,LD=1/5,GRN=1/5,BXT=1/5,Others=1/5):
self.labour += amount
self.conservative += -amount * Con
self.libDem += -amount * LD
self.green += -amount * GRN
self.brexit += -amount * BXT
self.others += -amount * Others

def addLD(self,amount, Con=1/5,Lab=1/5,GRN=1/5,BXT=1/5,Others=1/5):
self.libDem += amount
self.conservative += -amount * Con
self.labour += -amount * Lab
self.green += -amount * GRN
self.brexit += -amount * BXT
self.others += -amount * Others

def addGRN(self,amount, Con=1/5,Lab=1/5,LD=1/5,BXT=1/5,Others=1/5):
self.green += amount
self.conservative += -amount * Con
self.labour += -amount * Lab
self.libDem += -amount * LD
self.brexit += -amount * BXT
self.others += -amount * Others

def addBXT(self,amount, Con=1/5,Lab=1/5,LD=1/5,GRN=1/5,Others=1/5):
self.brexit += amount
self.conservative += -amount * Con
self.labour += -amount * Lab
self.libDem += -amount * LD
self.green += -amount * GRN
self.others += -amount * Others

def addOthers(self,amount, Con=1/5,Lab=1/5,LD=1/5,GRN=1/5,BXT=1/5):
self.others += amount
self.conservative += -amount * Con
self.labour += -amount * Lab
self.libDem += -amount * LD
self.green += -amount * GRN
self.brexit += -amount * BXT

c = Population("UK",100000)
d = [c.conservative,c.labour,c.libDem,c.green,c.brexit,c.others]
while not c.rulingParty()=="Labour":
c.addCon(-1000)
d = np.vstack((d,[c.conservative,c.labour,c.libDem,c.green,c.brexit,c.others]))


print(d)
print(np.shape(d)[0])

应该指出的是,这只是我为了提高 Python 技能而做的练习,而不是任何类型的政治模拟。我的问题是,有什么方法可以减少 addCon()、addLab() 等函数中重复行的数量,因为它们都有很多相同的代码。

如有任何反馈,我们将不胜感激您的,

最佳答案

绝对有,但它涉及一点点额外的工作。

首先,我不会将 libDemconservative 作为类中的字段。相反,我会让它们成为字典中的键,并将投票作为值。有点像您在 ruling_party 方法中所做的那样。但是在 __init__ 中设置字典,然后您可以重写您的方法。

事实上,您可以将它们全部合并到一个方法中:

def add_party(self, party, amount, adjustment_factors):
self.votes[party] += amount
for other_party in self.votes:
if other_party != party:
factor = adjustment_factors.get(other_party, 1/5)
self.votes[other_party] -= amount * factor

关于python - 有没有办法减少类中大量重复的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59396414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com