我正在尝试实现一个程序,该程序使用Python对象来表示数据来执行计算。本质上,我想弄清楚在上一次选举中投票的人口比例。我还想确定在前一次选举中投票率最高的县的名字,以及投票人数的百分比。
def highest_turnout(data) :
class County :
def__init__(self, init_name, init_population, init_voters) :
self.name = init_name
self.population = init_population
self.voters = init_voters
return # modify this as needed
# program evaluated using these objects
allegheny = County("allegheny", 1000490, 645469)
philadelphia = County("philadelphia", 1134081, 539069)
montgomery = County("montgomery", 568952, 399591)
lancaster = County("lancaster", 345367, 230278)
delaware = County("delaware", 414031, 284538)
chester = County("chester", 319919, 230823)
bucks = County("bucks", 444149, 319816)
data = [allegheny, philadelphia, montgomery, lancaster, delaware, chester, bucks]
result = highest_turnout(data)
print(result) # prints the output of the function^这是我上面的当前代码。我输入了数据,然后为县创建了一个类。我在这部分遇到了麻烦:
attributes
如有任何帮助/建议,将不胜感激。很高兴在评论中进一步澄清问题。非常感谢您,因为我是Python的新手:)。
发布于 2020-06-23 02:49:38
你可以按投票率最高的人对数据进行排序,如:
turnout = sorted(data, key=lambda x: x.voters/x.population, reverse=True)这将按递减顺序排序,这意味着投票率最高的县将是turnout[0].name。
产出:
chester你想要的元组可能是:
highest = turnout[0]
print((highest.name, highest.voters/highest.population))产出:
('chester', 0.7215045058280377)发布于 2020-06-23 04:43:43
我已经创建了一个程序,它看起来确实有点复杂,但一旦你阅读了它的全部,你可能能够理解它。这不是一个完全完成的程序,但它完成了工作。我到处添加了一些注释,以澄清一些代码。
#creating a dictionary to store the country name and its percentage
data = {}
#creating the class county
class County:
def __init__(self,country,population,voters):
self.country = country
self.voters = voters
self.population = population
self.sorted_data = ""
self.formatted_percentage = ""
def update_data(self):
#this method calculates the percentages to 2 decimal points and adds the country and percentage to the dictionary
percentage = self.voters/self.population
self.formatted_percentage = "{:.2f}".format(percentage)
#self.formatted percentage basically takes the calculated percentage and changes it to 2 decimal places
data.update({self.country: self.formatted_percentage})
#this .update method allows for the country and formatted percentage to be stored in the data dictionary
def sort_data(self):
#this is where the actually sorting and printing takes place
self.sorted_data = sorted(data.items(), key=lambda x: x[1], reverse=True)
#inside the self.sorted data, we are sorting the list, through the sorted method
# we then put in the parameters that we are using
#we are using the items in the data dictionary, our key is lamba,
#and reverse basically means that the order will start from highest to lowest
print(self.sorted_data)
#we then print the actual data, if you do not like how it looks when printed, you can simply add a for loop
#to loop through the dictionary and print it instead.
allegheny = County("allegheny", 1000490, 645469)
philadelphia = County("philadelphia", 1134081, 539069)
montgomery = County("montgomery", 568952, 399591)
lancaster = County("lancaster", 345367, 230278)
delaware = County("delaware", 414031, 284538)
chester = County("chester", 319919, 230823)
bucks = County("bucks", 444149, 319816)
allegheny.update_data()
philadelphia.update_data()
montgomery.update_data()
lancaster.update_data()
delaware.update_data()
chester.update_data()
bucks.update_data()
allegheny.sort_data()https://stackoverflow.com/questions/62526410
复制相似问题