且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将字符串添加到列表

更新时间:2023-02-23 09:24:39

字符串是可迭代的:元素是字符串的字符.在将可迭代项添加到列表时,可迭代项的 elements 会附加到列表中.

Strings are iterable: the elements are the string's characters. When you add an iterable to a list, the iterable's elements get appended to the list.

以下任何一项都可以满足您的期望(即追加字符串,而不用字符串的字符扩展列表):

Either of the following will do what you're expecting (i.e. append the string, not extend the list with the string's characters):

b += [c]

b.append(c)