从2 1维列表在python中创建2维列表

时间:2019-02-13 02:43:20

标签: python list

在我的程序中,我创建了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()

2 个答案:

答案 0 :(得分:0)

只需将您的列表添加到新列表中?

Save.write(passwordDataArrayList.get(i).returnKey().toString());

顺便说一句:列表foo = [winners, yearlist] 完全没有用,因为您存储的内容只有每个获胜者的索引+ 1903。

答案 1 :(得分:0)

或者制作一个l之类的[[],[]]列表,然后在循环中做一些小改动:

def main():
    l = [[],[]]
    year = 1903


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

main()