Python字典的总和(时间/空间复杂度)

时间:2018-08-30 17:16:40

标签: python python-3.x dictionary time-complexity space-complexity

我正在尝试解决以下问题:

给出出生日期和死亡日期的列表,找到大多数人还活着的年份。

到目前为止,这是我的代码:

b = [1791, 1796, 1691, 1907, 1999, 2001, 1907] # birth dates
d = [1800, 1803, 1692, 1907, 1852, 1980, 2006] # death dates

year_dict = {} # populates dict key as year, val as total living/dead
for birth in b:
    year_dict.setdefault(birth,0) # sets default value of key to 0 
    year_dict[birth] += 1 # will add +1 for each birth and sums duplicates
for death in d:
    year_dict.setdefault(death,0) # sets default value of key to 0
    year_dict[death] += -1 # will add -1 for each death and sums duplicates

以下代码返回:

{1791: 1, 1796: 1, 1691: 1, 1907: 1, 1999: 1, 2001: 1, 1800: -1, 1803: -1, 1692: -1, 1852: -1, 1980: -1, 2006: -1}

现在,我正在寻找一种方法来创建一个连续的总和,以找出哪一年居住的人口最多,例如:

Image of desired result

我们可以看到,根据给定的数据集,结果表明1796年的人口最多。我在获取运行总和部分时遇到麻烦,该部分需要采用每个键值,并将其与先前的值相加。我尝试了几种不同的循环和枚举,但现在陷入了困境。一旦找到解决问题的最佳方法,我将创建一个提高效率的函数。

如果考虑到时间/空间的复杂性,有一种更有效的方法,请告诉我。我正在尝试学习python的效率。非常感谢您的帮助!!!

2 个答案:

答案 0 :(得分:1)

您是否要在其中容纳结果的特定数据结构?我得到了与imgur链接相同的结果,可以打印到终端。不过,将其写入字典并不难。

from collections import OrderedDict

b = [1791, 1796, 1691, 1907, 1999, 2001, 1907] # birth dates
d = [1800, 1803, 1692, 1907, 1852, 1980, 2006] # death dates

year_dict = {} # populates dict key as year, val as total living/dead
for birth in b:
    year_dict.setdefault(birth,0) # sets default value of key to 0 
    year_dict[birth] += 1 # will add +1 for each birth and sums duplicates
for death in d:
    year_dict.setdefault(death,0) # sets default value of key to 0
    year_dict[death] += -1 # will add -1 for each death and sums duplicates

year_dict = OrderedDict(sorted(year_dict.items(), key=lambda t: t[0]))
solution_dict = {}

total = 0
print('year net_living running_sum')
for year in year_dict:
    total += year_dict[year]
    solution_dict.update({year:{'net_living': year_dict[year],
                                'running_sum': total}
                                })
    print('{} {:4} {:10}'.format(year, year_dict[year], total))

输出:

year net_living running_sum
1691    1          1
1692   -1          0
1791    1          1
1796    1          2
1800   -1          1
1803   -1          0
1852   -1         -1
1907    1          0
1980   -1         -1
1999    1          0
2001    1          1
2006   -1          0

solution_dict的输出

{
1691: {'net_living':  1, 'running_sum':  1},
1692: {'net_living': -1, 'running_sum':  0},
1791: {'net_living':  1, 'running_sum':  1},
1796: {'net_living':  1, 'running_sum':  2},
1800: {'net_living': -1, 'running_sum':  1},
1803: {'net_living': -1, 'running_sum':  0},
1852: {'net_living': -1, 'running_sum': -1},
1907: {'net_living':  1, 'running_sum':  0},
1980: {'net_living': -1, 'running_sum': -1},
1999: {'net_living':  1, 'running_sum':  0},
2001: {'net_living':  1, 'running_sum':  1},
2006: {'net_living': -1, 'running_sum':  0}
}

答案 1 :(得分:1)

我将使用pandas,并利用其DataFrame对象:

制作人的出生年份和死亡年份的数据框:

born = [1791, 1796, 1691, 1907, 1999, 2001, 1907] # birth dates
died = [1800, 1803, 1692, 1907, 1852, 1980, 2006] # death dates
people = pd.DataFrame({'born': born, 'died': died} for born, died in zip(born, died))

制作一个数据框,其中包含第一个列出的出生日期和最后一个列出的死亡之间的所有年份:

years = pd.DataFrame(index=np.arange(people['born'].min(), people['died'].max() + 1))

查找这些年份中每年活着的总人数:

for year in years.index:
    num_living = ((year > people['born']) & (year < people['died'])).sum()
    years.loc[year, 'total_living'] = num_living

呼叫years.tail()会产生以下结果:

    total_living
2002    1.0
2003    1.0
2004    1.0
2005    1.0
2006    0.0

从那里,您只需在argmax列上执行'total_living'

要清楚,我假设一个合理的情况是,人们出生后 死亡,并且(因此)永远没有负数的人活着。