Python split for lists

前端 未结 6 1316
眼角桃花
眼角桃花 2020-11-28 12:43

If we have a list of strings in python and want to create sublists based on some special string how should we do?

For instance

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 13:16

        lst = ["data","more data","","data 2","more data 2","danger","","date3","lll"]
        join_list = ",".join(lst)
        split_list = join_list.split(",,")
        result = [i.split() for i in split_list]
        #result =[['data,more', 'data'], ['data', '2,more', 'data', '2,danger'],  ['date3,lll']]
    

提交回复
热议问题