首页 > 解决方案 > 从 2 个 1 维列表在 python 中创建 2 维列表

问题描述

在我的程序中,我创建了 2 个一维列表。我需要将这 2 个列表组合成一个二维列表。我不知道该怎么做。到目前为止,这是我创建 2 个列表的代码:

def main():
    winners = []
    year = 1903
    yearlist = [] 


    with open('WorldSeriesWinners.txt') as myfile:
         for line in myfile:
             line = line.rstrip('\n')
             winners.append(line)
    for num in range(len(winners)):
         yearlist.append(year)
         year += 1
    print(winners)
    print (yearlist)

 main()

标签: pythonlist

解决方案


Just add your list to new list?

foo = [winners, yearlist]

BTW: list yearlist is completely useless as what you store there is just each winner's index + 1903.


推荐阅读