【问题标题】:How to split 2 items of an array and join into one item?如何拆分数组的两项并加入一项?
【发布时间】:2018-02-16 10:48:33
【问题描述】:

我制作了一个包含 2 个项目的数组的 Python 程序。我想将这 2 项拆分并合并为一项。所以结果应该是“第一项,第二项”。它给出了错误 AttributeError: 'list' object has no attribute 'split'。我附上了代码。

items_in_cart=['first item','second item']
a,b=items_in_cart.split(", ")
items_in_cart=a+b
print(items_in_cart)

【问题讨论】:

  • 您不能拆分列表。您必须将其转换为字符串或仅使用items_in_cart=items_in_cart[0]+items_in_cart[1]
  • 好的!感谢您的帮助!

标签: python arrays


【解决方案1】:

试试这个:

', '.join(items_in_cart)

您的列表中已有项目拆分。你想再次加入他们。

【讨论】:

  • 完美!谢谢!
猜你喜欢
  • 2010-12-01
  • 1970-01-01
  • 2015-07-11
  • 1970-01-01
  • 1970-01-01
  • 2018-10-21
  • 1970-01-01
  • 2019-01-02
  • 1970-01-01
相关资源
最近更新 更多