解释以下python代码中第三行listtempappendaddnew这一行代码将addnew作为一个元素添加到了temp的列表中为什么返回值为None:temp = 23 33 91 100 9999temp = temp4temp4 = listtempappendaddnewprinttemp4
list(temp)将元组temp转换为一个列表。然后,.append(["add","new"])是对这个列表进行添加操作,将["add","new"]作为一个元素添加到了列表中。但是需要注意的是,.append()方法执行后会直接修改列表,而不会返回新的列表。
因此,temp4实际上被赋值为None,因为list(temp).append(["add","new"])的返回值为None。
如果想要得到添加元素后的新列表,可以使用以下代码:
temp = ("23", "33", "91", "100", "9999")
temp = temp[:4]
temp_list = list(temp)
temp_list.append(["add","new"])
print(temp_list)
这样就能得到添加元素后的新列表['23', '33', '91', '100', ['add', 'new']]。
原文地址: http://www.cveoy.top/t/topic/iHeD 著作权归作者所有。请勿转载和采集!