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.Trybaz+=[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']
>>>