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