【问题标题】:Append a string to a list of unicode strings将字符串附加到 unicode 字符串列表
【发布时间】:2012-09-14 09:15:44
【问题描述】:

我尝试过的:

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

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

我想要什么:

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

[u'abcd', 'ef']

希望我已经说得够清楚了!

【问题讨论】:

  • 尝试将值作为列表提供或使用 append 添加到列表中
  • 您忘记使用--do-magic 编译您的Python 解释器,因此您无法编写**MAGIC ???** 并让代码正常工作。 :)

标签: python string list unicode concatenation


【解决方案1】:

列出清单:

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

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

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

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

这都是标准的python列表操作,与内容无关;该列表中是否有 unicode 对象或自定义对象都没有关系。

【讨论】:

    猜你喜欢
    • 2012-12-05
    • 2018-09-13
    • 2012-07-12
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多