将字符串附加到unicode字符串列表

时间:2012-09-21 22:57:11

标签: python string list unicode concatenation

我尝试了什么:

>> abcd = [u'abcd']
>> abcd_ef = abcd + 'ef'
>> abcd_ef

[u'abcd', 'e', 'f']

我想要的是什么:

>> abcd = [u'abcd']
>> abcd_ef = **MAGIC ???**
>> abcd_ef

[u'abcd', 'ef']

希望我说得那么清楚!

1 个答案:

答案 0 :(得分:4)

将其列为清单:

>>> abcd = [u'abcd']
>>> abcd_ef = abcd + ['ef']
>>> abcd_ef
[u'abcd', 'ef']

否则列表会分别添加字符串的每个元素(例如每个字符)。

或者,您可以在.append()上致电abcd并就地修改该列表:

>>> abcd = [u'abcd']
>>> abcd.append('ef')
>>> abcd
[u'abcd', 'ef']

这是所有标准的python列表操作,并且与内容无关;如果该列表中有unicode对象或自定义对象,则无关紧要。