首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为政府数据创建Python程序

为政府数据创建Python程序
EN

Stack Overflow用户
提问于 2020-06-23 02:34:05
回答 2查看 263关注 0票数 1

我正在尝试实现一个程序,该程序使用Python对象来表示数据来执行计算。本质上,我想弄清楚在上一次选举中投票的人口比例。我还想确定在前一次选举中投票率最高的县的名字,以及投票人数的百分比。

代码语言:javascript
复制
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

  • Returning
  • 查找投票率最高的县,即投票率最高的县,使用对象的人口和投票者组成的元组,其中包含投票率最高的县的名称和投票的人口百分比,按该顺序(百分比表示为0到1之间)

如有任何帮助/建议,将不胜感激。很高兴在评论中进一步澄清问题。非常感谢您,因为我是Python的新手:)。

EN

回答 2

Stack Overflow用户

发布于 2020-06-23 02:49:38

你可以按投票率最高的人对数据进行排序,如:

代码语言:javascript
复制
turnout = sorted(data, key=lambda x: x.voters/x.population, reverse=True)

这将按递减顺序排序,这意味着投票率最高的县将是turnout[0].name

产出:

代码语言:javascript
复制
chester

你想要的元组可能是:

代码语言:javascript
复制
highest = turnout[0]
print((highest.name, highest.voters/highest.population))

产出:

代码语言:javascript
复制
('chester', 0.7215045058280377)
票数 1
EN

Stack Overflow用户

发布于 2020-06-23 04:43:43

我已经创建了一个程序,它看起来确实有点复杂,但一旦你阅读了它的全部,你可能能够理解它。这不是一个完全完成的程序,但它完成了工作。我到处添加了一些注释,以澄清一些代码。

代码语言:javascript
复制
#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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62526410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档