Join a string using delimiters

后端 未结 24 1601
北海茫月
北海茫月 2020-12-05 09:57

What is the best way to join a list of strings into a combined delimited string. I\'m mainly concerned about when to stop adding the delimiter. I\'ll use C# for my example

24条回答
  •  粉色の甜心
    2020-12-05 10:55

    For python be sure you have a list of strings, else ','.join(x) will fail. For a safe method using 2.5+

    delimiter = '","'
    delimiter.join(str(a) if a else '' for a in list_object)
    

    The "str(a) if a else ''" is good for None types otherwise str() ends up making then 'None' which isn't nice ;)

提交回复
热议问题