Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将强制转换的字符串添加到字符串数组_Python_List_Python 3.x - Fatal编程技术网

Python 将强制转换的字符串添加到字符串数组

Python 将强制转换的字符串添加到字符串数组,python,list,python-3.x,Python,List,Python 3.x,我有以下代码(简化): 这为条提供了以下值: ["foo", "0", "1", "2", "3", "4"] 但是,我需要条的以下值: ["foo", "01234"] 有办法吗 处理列表时,+=操作符的行为类似于list.extend。换句话说,它将获取baz字符串中的字符,并将它们逐个附加到栏列表中 要将baz字符串作为一个整体附加,请使用: 下面是一个演示: >>> lst = ['a', 'b', 'c'] >>> lst += 'def'

我有以下代码(简化):

这为
提供了以下值:

["foo", "0", "1", "2", "3", "4"]
但是,我需要
的以下值:

["foo", "01234"]  

有办法吗

处理列表时,
+=
操作符的行为类似于
list.extend
。换句话说,它将获取
baz
字符串中的字符,并将它们逐个附加到
列表中

要将
baz
字符串作为一个整体附加,请使用:

下面是一个演示:

>>> lst = ['a', 'b', 'c']
>>> lst += 'def'  # Appends individual characters
>>> lst
['a', 'b', 'c', 'd', 'e', 'f']
>>>
>>> lst = ['a', 'b', 'c']
>>> lst.append('def')  # Appends whole string
>>> lst
['a', 'b', 'c', 'def']
>>>

因为+=调用list.extend,所以使用list.append.Try
baz+=[str(i)]
bar+=[baz]
bar.append(baz)
>>> lst = ['a', 'b', 'c']
>>> lst += 'def'  # Appends individual characters
>>> lst
['a', 'b', 'c', 'd', 'e', 'f']
>>>
>>> lst = ['a', 'b', 'c']
>>> lst.append('def')  # Appends whole string
>>> lst
['a', 'b', 'c', 'def']
>>>