在Python中为CSV输出附加列表

目前,我正在从网络上抓取数据,并希望将其输出为CSV。一切工作正常,但是一旦我在迭代中追加多个列表,该列表的格式就会错误。


我从这样的事情开始:


list = [a, b, c]

list_two = [d, e, f]

list_three = [g, h, i]

第一次迭代:


list = [list, list_two]

# list = [[a, b, c], [d, e, f]]

第二次迭代:


list = [list, list_three]

我得到:


# list = [[[a, b, c], [d, e, f]], [g, h, i]]

我希望有:


# list = [[a, b, c], [d, e, f], [g, h, i]]

请帮我!我想这是一件容易的事,但我不明白。而且我实际上很难找到有关如何附加列表的信息。


守着一只汪
浏览 151回答 2
2回答

小唯快跑啊

只需使用+连接两个列表:list = [ list, list_two ]list += [ list_three ]您还可以使用append:list = [ list ]list.append( list_two )list.append( list_three )

慕桂英3389331

您可以创建一个助手列表并使用append:例如helperList = []list = ['a', 'b', 'c']list_two = ['d', 'e', 'f']list_three = ['g', 'h', 'i']helperList.append(list)helperList.append(list_two)helperList.append(list3_three)#helperList >>> [['a', 'b', 'c'], ['d', 'e', 'g'], ['g', 'h', 'i']]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python