如何在新列表中连接None和string?
>>> a = None
>>> b = 'apple,banana,cherry'
>>> new_list = a + b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
>>>
>>> new_list = [None, 'apple,banana,cherry']
>>> print(new_list)
[None, 'apple,banana,cherry']
>>> print(type(new_list))
<class 'list'>
>>>
答案 0 :(得分:1)
将项目添加到要使用append方法的列表中:
my_list = []
a = None
b = 'apple,banana,cherry'
# adds a to the list
my_list.append(a)
# adds b to the list
my_list.append(b)