python - 如何在python中追加两个字符串数组

标签 python

我有两个字符串列表,我正在尝试附加两个列表并想在字符串末尾添加竖线 (|) 分隔符。请在下面找到我的代码:

firstList = ['Product1,item,,description,price|', 'Product2,item,,description,price|','Product3,item,,description,price|']
secondList = 'Product4,item,,description,price'

result = firstlist + secondList
result.append('|')
print result

我想像下面这样打印我的输出:

Product1,item,,description,price|Product2,item,,description,price|Product3,item,,description,price|Product4,item,,description,price|

但我的代码没有附加竖线分隔符属性,它作为列表中的单独字符串值附加。在现有列表中附加新值时,我看到了我不想要的空白区域。

有人可以帮忙解决这个问题吗?如果有人可以教育在 python 中附加两个列表的有效方法是什么,那将非常有帮助。

谢谢。

最佳答案

你可以试试这个:

firstList = ['Product1,item,,description,price|', 'Product2,item,,description,price|','Product3,item,,description,price|']
secondList = secondList = 'Product4,item,,description,price'
final_list = ''.join(firstList+[secondList[0]+"|"]) if isinstance(secondList, list) else ''.join(firstList+[secondList+"|"])

输出:

'Product1,item,,description,price|Product2,item,,description,price|Product3,item,,description,price|Product4,item,,description,price|'

关于python - 如何在python中追加两个字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47116871/

相关文章:

python - 属性错误 : 'scoped_session' object has no attribute 'session'

python - 功能变化不大。面具。 Python

python - 编写一个快速函数来检查矩阵中的元素

c++ - Boost 和 Python 3.x

python - python 2 和 python 3 之间的 read() 区别

python - python的控制台应用程序gui

Python 模块 : functions calling each other

python - pygame.错误: Text has zero width; what's a better way to make it work?

python - "blocking"这个词在编程中是什么意思?

Python 2.6 : Class inside a Class?