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
输出:
无
Output: None
推荐答案
追加
不会返回任何内容,所以您不能说
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屋!