Python 哪一年活着的人数最多

Python 哪一年活着的人数最多,python,Python,为列表中的人指定出生年份和死亡年份(出生1、死亡1、出生2、死亡2…),哪一年的活着人数最多 我在考虑如何用正确的方法解决这个问题吗?我不是一个程序员,我只是从这个问题中得到乐趣。这是我试图解决的问题 list1 = [1900, 1990, 1950, 1997, 1937, 1961, 1912, 1919, 1927, 1981] initial_year = 0 alive = 0 for i in range(1900,2000): count = len(list1)/2

为列表中的人指定出生年份和死亡年份(出生1、死亡1、出生2、死亡2…),哪一年的活着人数最多

我在考虑如何用正确的方法解决这个问题吗?我不是一个程序员,我只是从这个问题中得到乐趣。这是我试图解决的问题

list1 = [1900, 1990, 1950, 1997, 1937, 1961, 1912, 1919, 1927, 1981]

initial_year = 0
alive = 0
for i in range(1900,2000):
    count = len(list1)/2
    for j in list1:
        if j%2 == 1 and j<=i:
            count = count - 1
            if count>alive:
                initial_year = i
                alive = count

return initial_year
list1=[1900、1990、1950、1997、1937、1961、1912、1919、1927、1981]
初始年=0
活动=0
对于范围内的i(19002000):
计数=len(列表1)/2
对于清单1中的j:
如果j%2==1,且j为:
初始年=i
活着=计数
返回初始年

既然您只是为了好玩而尝试解决它,这里有一个效率低但简单的算法:将问题分成两部分

  • 确定每年有多少人活着
  • 找出生前人数最多的年份

  • 朴素的实现如下所示:

    lst = [1900, 1990, 1950, 1997, 1937, 1961, 1912, 1919, 1927, 1981]
    
    birth_years = lst[0::2]
    # extended slice notation means: start with the first element, end with the 
    # last element, and take every second element in between
    death_years = lst[1::2]
    # start with the second, end with the last, and take every second
    
    alive = 0
    max_alive = (0, None)  # (num_alive, year)
    
    for year in range(min(birth_years), max(death_years)+1):
        alive += birth_years.count(year)
        alive -= birth_years.count(year)
        if alive > max_alive[0]:
            max_alive = (alive, year)
    
    更健壮的实现可能更像:

    import collections
    
    Person = collections.namedtuple("Person", "born died")
    # define a structure with two attributes: "born" and "died"
    
    people = [Person(born, died) for born, died in zip(lst[0::2], lst[1::2])]
    # build a list of people
    
    years = {y: sum(1 for p in people if p.born <= y <= p.died)
             for y in range(min(lst), max(lst)+1)}
    # use a dictionary comprehension to see how many people are alive in each year
    
    result = max(years, key=lambda k: years[k])
    # use the max built-in with a key function to get the resulting year
    
    导入集合
    Person=collections.namedtuple(“Person”,“born-dead”)
    #定义具有两个属性的结构:“出生”和“死亡”
    people=[Person(born,dead)for born,dead in zip(lst[0::2],lst[1::2])]
    #建立一个人员列表
    
    年份={y:sum(如果在JavaScript中p.born,则人中p为1

    function writeOutput(inputArray) {
    
    
    let n = inputArray.length;
    let births = [];
    let deaths = [];
    let alive = 1;
    let max_alive = 1;
    let ix = 1;
    let j = 0;
    
    
    for ( i=0; i<inputArray.length ; i++){
        if ((i+2)%2 == 0){
            births.push(inputArray[i]);
        } else {
            deaths.push(inputArray[i]);
        }
    }
    
    let birthsSorted = births.sort();
    let deathsSorted = deaths.sort();
    let year = birthsSorted[0];
    while (ix < n && j < n ){
        if (birthsSorted[ix] <= deathsSorted[ix]){
            alive += 1;
            if(alive > max_alive){
                max_alive = alive;
                year = birthsSorted[ix]
            }
            ix++;
            console.log(alive)
        } else {
            alive = alive - 1;
            j++;
        }
    }
    
    
    return year;   }
    
    函数写输出(输入阵列){
    设n=inputArray.length;
    让出生率=[];
    让死亡=[];
    让活着=1;
    让max_活着=1;
    设ix=1;
    设j=0;
    
    对于(i=0;i请不要在屏幕截图中发布代码。相反,您的发布、复制粘贴代码、选择代码,然后按Ctrl+KI组合键按您的建议进行操作。我不确定这是否会有所不同。啊,这要好得多。我最初的错误假设是,每个人都会从每年都活着开始。