如何根据索引位置将列表列表连接到另一个小列表?

2024-05-29 05:41:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要一个列表和另一个列表,并在它们之间建立关系。就上下文而言,我不允许使用熊猫或字典之类的东西

我打开了两个文件并创建了两个函数,第一个函数将文件与年份和分配给该年份的12个就业机会创造值一起转换,形成一个列表。下一个文件是总统的名字和他们服务的年限,第二个功能是我把文件中的数据做成一个列表

我必须将他们的政党和总统任期与他们服务的年份联系起来,当我完成向总统分配名单时,我必须对他们进行平均

我需要帮助的是以某种方式合并这些名单,这样我就可以让每一位总统都有他们指定的年份和当年的数据。我需要的一个例子是: [James Earl Carter, 1979-1981, Democrat, 140990, 14135, etc...]他服务了这么多年。 `

以下是两个文件的示例:

1979,14090,14135,14152,14191,14221,14239,14288,14328,14422,14484,14532,14559
1980,14624,14747,14754,14795,14827,14784,14861,14870,14824,14900,14903,14946
1981,14969,14981,14987,14985,14971,14963,14993,15007,14971,15028,15073,15075
1982,15056,15056,15050,15075,15132,15207,15299,15328,15403,15463,15515,15538
1983,15611,15671,15731,15797,15834,15852,15901,15891,15819,15858,15894,15911
1984,15937,15947,15956,15977,15990,16045,16150,16229,16128,16136,16173,16180
1985,16201,16226,16296,16583,16454,16441,16418,16410,16330,16386,16391,16373
1986,16360,16346,16292,16260,16198,16159,16175,16110,16031,16069,16078,16073
1987,16041,16011,16024,16010,16003,16016,15890,15930,15923,15956,15977,15981
1988,16023,16004,16005,15990,16005,16020,16011,16016,16042,15986,15997,16008
1989,16010,16025,16030,16075,16103,16127,16172,16224,16255,16274,16311,16282

James Earl Carter, 1979-1981, Democrat
Ronald Wilson Reagan, 1981-1989, Republican

以下是我目前掌握的代码:

def avg():
      file = open("government_employment_Windows.txt")
      my_list = []

      for line in file:
          line.strip()
          line = line.split(',')
          line[1] = int(line[1])
          line[2] = int(line[2])
          line[3] = int(line[3])
          line[4] = int(line[4])
          line[5] = int(line[5])
          line[6] = int(line[6])
          line[7] = int(line[7])
          line[8] = int(line[8])
          line[9] = int(line[9])
          line[10] = int(line[10])
          line[11] = int(line[11])
          line[12] = int(line[12])

          my_list.append(line)
    file.close()
    return my_list


def pres(list_of_lists):
    p_file = open("presidents_Windows.txt")
    print(list_of_lists)

    for line in p_file:
        line = line.strip()
        line = line.split(',')
        line[1] = line[1].strip()
        line[2] = line[2].strip()
    print(line)

    for small_list in list_of_lists:
        if line[0] in small_list:
            small_list.append(line[0])
    print(list_of_lists)
list_of_lists = avg()
pres(list_of_lists)

最终结果应如下所示:

Government employement average per month:
    Republican: 18562
    Democrat: 19599


Government Employment by President:
     First Month    Last Month     Difference
Carter: 14090          14946
Reagan: 14969           16008
Bush: 16010             17347
Clinton: 17365          19466
Bush: 19450             21546
Obama: 21538            22266
Trump: 22264            21902

Tags: 文件ofin列表mylinelistslist
2条回答

下面是一个让您开始学习的示例:

from io import StringIO

# sample data, these behave like a file,
# as if you did government_employment = open('filename.txt')
f_government_employment = StringIO("""1979,14090,14135,14152,14191,14221,14239,14288,14328,14422,14484,14532,14559
1980,14624,14747,14754,14795,14827,14784,14861,14870,14824,14900,14903,14946
1981,14969,14981,14987,14985,14971,14963,14993,15007,14971,15028,15073,15075
1982,15056,15056,15050,15075,15132,15207,15299,15328,15403,15463,15515,15538
1983,15611,15671,15731,15797,15834,15852,15901,15891,15819,15858,15894,15911
1984,15937,15947,15956,15977,15990,16045,16150,16229,16128,16136,16173,16180
1985,16201,16226,16296,16583,16454,16441,16418,16410,16330,16386,16391,16373
1986,16360,16346,16292,16260,16198,16159,16175,16110,16031,16069,16078,16073
1987,16041,16011,16024,16010,16003,16016,15890,15930,15923,15956,15977,15981
1988,16023,16004,16005,15990,16005,16020,16011,16016,16042,15986,15997,16008
1989,16010,16025,16030,16075,16103,16127,16172,16224,16255,16274,16311,16282""")

f_presidents = StringIO("""James Earl Carter, 1979-1981, Democrat
Ronald Wilson Reagan, 1981-1989, Republican""")


# ideally, you'd use classes for something like this, but since the exercise appears
# to be about doing it with basic data structures, just using functions and lists:

def read_employment(f):
    # reads the employment 'file' into a list of lists that pairs up a year and a
    # list of months of employment
    for line in f:
        line = [int(x) for x in line.strip().split(',')]
        # you'd use tuples, but since you want lists only
        yield [line[0], line[1:]]


def get_employment(employment, year, month):
    # given an iterable (like a list) created from the read_employment generator,
    # gets the employment for a specific year and month combo
    for e in employment:
        if e[0] == year:
            # month-1, to have months from 1-12
            return e[1][month-1]


def read_presidents(f):
    for line in f:
        line = line.split(',')
        term = [int(x) for x in list(line[1].strip().split('-'))]
        yield [line[0].strip(), term, line[2].strip()]


def get_term_months(presidents, president):
    for p in presidents:
        if president in p[0]:
            for year in range(p[1][0], p[1][1]):
                for month in range(1, 13):
                    # skip January of the first year, the previous president still going
                    if month != 1 or year != p[1][0]:
                        yield year, month
            # only January of the list year
            yield [p[1][1], 1]


def main():
    government_employment = list(read_employment(f_government_employment))
    # for example, showing the government employment for March, 1981
    print(get_employment(government_employment, 1981, 3))

    presidents = list(read_presidents(f_presidents))
    # for example, showing all the months for Carter
    print(list(get_term_months(presidents, 'Carter')))

    # now, you can do things like compute the average government employment
    # during the Carter presidency
    values = [
        get_employment(government_employment, year_month[0], year_month[1])
        for year_month in get_term_months(presidents, 'Carter')
    ]
    print(f'Average government employment during the Carter presidency: {sum(values)/len(values)}')


if __name__ == '__main__':
    main()

注意,这里还有一些丑陋的地方。例如,如果您不仅使用列表,还使用元组,例如:

    values = [
        get_employment(government_employment, year_month[0], year_month[1])
        for year_month in get_term_months(presidents, 'Carter')
    ]

如下所示(假设get_term_months现在将返回一个元组):

    values = [
        get_employment(government_employment, year, month)
        for year, month in get_term_months(presidents, 'Carter')
    ]

这远远超出了您的问题范围,但为了给您一种感觉,使用类将允许您以更好的方式将内容组合在一起。例如,这就是就业信息开始呈现的样子:

class Employment:
    def __init__(f):
        self.data = [int(x) for x in line.strip().split(',') for line in f]

    def get_employment(employment, year, month):
        for e in self.data:
            if e[0] == year:
                return e[month]


government_employment = Employment(f_government_employment)

因为这是一个家庭作业问题,我不会为你解决整个问题;然而,我将根据一位总统的任职年限,讨论如何将这些名单合并在一起。您可以获取下面的输出,并按照您喜欢的方式进行格式化,并且很容易获取平均值

首先,假设您在各自的列表中获得了职位和总裁(当您从文件中清除数据时),如下所示:

jobs = [
    [1979,14090,14135,14152,14191,14221,14239,14288,14328,14422,14484,14532,14559],
    [1980,14624,14747,14754,14795,14827,14784,14861,14870,14824,14900,14903,14946],
    [1981,14969,14981,14987,14985,14971,14963,14993,15007,14971,15028,15073,15075],
    [1982,15056,15056,15050,15075,15132,15207,15299,15328,15403,15463,15515,15538],
    [1983,15611,15671,15731,15797,15834,15852,15901,15891,15819,15858,15894,15911],
    [1984,15937,15947,15956,15977,15990,16045,16150,16229,16128,16136,16173,16180],
    [1985,16201,16226,16296,16583,16454,16441,16418,16410,16330,16386,16391,16373],
    [1986,16360,16346,16292,16260,16198,16159,16175,16110,16031,16069,16078,16073],
    [1987,16041,16011,16024,16010,16003,16016,15890,15930,15923,15956,15977,15981],
    [1988,16023,16004,16005,15990,16005,16020,16011,16016,16042,15986,15997,16008],
    [1989,16010,16025,16030,16075,16103,16127,16172,16224,16255,16274,16311,16282]
]

pres = [
    ["James Earl Carter", "1979-1981", "Democrat"],
    ["Ronald Wilson Reagan", "1981-1989", "Republican"]
]

出于演示目的,让我们在单独的变量中定义carter:

carter = pres[0]

要合并此数据,可以定义两个函数。一个用于将“YYYY-yyy”转换为一系列整数,您可以使用这些整数合并总统服务的每一年的工作列表。另一个功能将用于实际合并列表

def to_range(years):
    r = [int(i) for i in years.split("-")]
    return [i for i in range(r[0], r[1]+1)]

上面的函数将以字符串作为输入,并希望它是“YYYY-yyy”形式。例如,如果我们想知道卡特任职的年限,我们可以说:

to_range(carter[1])

哪些产出:

[1979, 1980, 1981]

现在我们有了卡特服务的所有岁月,我们可以继续第二个功能。如果你想要一行,你可以用这个相当难看且冗长的列表:

def get_all_jobs(year_range, jobs):
    return [n[i] for n in jobs if n[0] in year_range for i in range(1, len(n))]

如果您正在寻找“更漂亮”的替代方案,那么使用extend也可以:

def get_all_jobs(year_range, jobs):
    all_jobs = []
    for n in jobs:
        if n[0] in year_range:
            all_jobs.extend(n[1:])
    return all_jobs

上面的函数创建一个合并列表。如果我们这样调用函数,通过Carter的范围:

carter_jobs = get_all_jobs(carter_years, jobs)

我们得到:

[14090, 14135, 14152, 14191, 14221, 14239, 14288, 14328, 14422, 14484, 14532, 14559, 14624, 14747, 14754, 14795, 14827, 14784, 14861, 14870, 14824, 14900, 14903, 14946, 14969, 14981, 14987, 14985, 14971, 14963, 14993, 15007, 14971, 15028, 15073, 15075]

相关问题 更多 >

    热门问题