如何在列表底部添加新列表?

时间:2019-06-15 08:45:48

标签: python

我有几个以垂直格式显示的列表,我试图在其底部添加一个新列表,但是每当尝试这样做时,我通常都会将新列表添加到列表中而不是其底部

我将列表解析为名称和高度变量:

for data in f:
    drawingdata = data.split()
    names = drawingdata[0]
    heights = drawingdata[1]
    names_list = names.strip('][').split(',')

以下是几个列表的示例:

Tom 1.65
Sharon 1.55
James 1.71
Jason 1.66
Jane 1.60

当我打印names_list变量时:

['Tom']
['Sharon']
['James']
['Jason']
['Jane']

我希望结果是:

['Tom']
['Sharon']
['James']
['Jason']
['Jane']
['John']

我尝试使用append()函数,但效果不佳,这是append函数的结果

['Tom', 'John']
['Sharon','John']
['James',"John']
['Jason','John']
['Jane','John']

我不知道该怎么做,请使用基本功能,不要导入,因为这是来自我不需要导入的测试

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

list_1 = [['Tom'], ['Sharon'], ['James'], ['Jason'], ['Jane']]
list_2 = [['John'], ['Eliza']]
new_list = list_1 + list_2
for i in new_list:
    ['Tom']
    ['Sharon']
    ['James'] 
    ['Jason']
    ['Jane']
    ['John']
    ['Eliza']