Python追加到列表返回无 [英] Python Append to a list returns none
问题描述
我知道已经回答了这个问题的不同版本,例如 [here].1 但我似乎无法让它在我的例子中工作.我正在尝试制作一个名为 ListB 的 ListA 副本,然后向 ListB 添加一个附加项目,即New_Field_Name",稍后我将其用作数据框的列标题.
I know different versions of this question has been answered for example [here].1 But I just couldn't seem to get it working on my example. I am trying to make a copy of ListA called ListB and then add an additional item to ListB i.e. 'New_Field_Name' in which later on I use as column headers for a dataframe.
这是我的代码:
ListA = ['BUSINESSUNIT_NAME' ,'ISONAME', 'Planning_Channel', 'Planning_Partner', 'Is_Tracked', 'Week', 'Period_Number']
print type(ListA)
输出:
ListB = list(ListA)
print '######', ListB, '#####'
输出:###### ['BUSINESSUNIT_NAME'、'ISONAME'、'Planning_Channel'、'Planning_Partner'、'Is_Tracked'、'Week'、'Period_Number'] #####
Output: ###### ['BUSINESSUNIT_NAME', 'ISONAME', 'Planning_Channel', 'Planning_Partner', 'Is_Tracked', 'Week', 'Period_Number'] #####
ListB = ListB.append('New_Field_Name')
print type(ListB)
print ListB
输出:无
推荐答案
append
不返回任何东西,所以你不能说
append
does not return anything, so you cannot say
ListB = ListB.append('New_Field_Name')
它就地修改了列表,所以你只需要说
It modifies the list in place, so you just need to say
ListB.append('New_Field_Name')
或者你可以说
ListB += ['New_Field_Name']
这篇关于Python追加到列表返回无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!