Python字典计算

时间:2018-02-08 11:21:43

标签: python python-3.x dictionary

鉴于以下字典(显示每年星期六发生的总出生人数):

public function load_Confrence_passes()
{
    //load the model
    $this->load->model('confrence_model');

    //load the method of model
    $data['h']=$this->confrence_model->confrencepasses();

    $this->load->vars($data);// https://www.codeigniter.com/user_guide/libraries/loader.html#CI_Loader::vars

    $this->load->view('mheader');
    $this->load->view('confrence/load_Confrence_passes');
    $this->load->view('mfooter');
}

如何计算连续年份之间的差异,以查看出生人数是逐年增加还是减少。

2 个答案:

答案 0 :(得分:1)

类似的东西:

POM

输出

data = {'1994': 474732, '1995': 459580, '1996': 456261, '1997': 450840, '1998': 453776, '1999': 449985, '2000': 469794, '2001': 453928, '2002': 445770, '2003': 447445} res = {} previous_year = None for k in sorted(data.keys()): if previous_year != None: res[k] = data[k] - data[previous_year] previous_year = k print repr(res)

答案 1 :(得分:0)

这样的事情:

componentDidMount

输出:

dict = {1994: 474732,
 1995: 459580,
 1996: 456261,
 1997: 450840,
 1998: 453776,
 1999: 449985,
 2000: 469794,
 2001: 453928,
 2002: 445770,
 2003: 447445}

diffs = {}
tmp = 0;
for i, k in enumerate(dict):
  diffs[k] = dict[k]-tmp
  tmp = dict[k]

print(diffs)

Live demo