How do I merge a 2D array in Python into one string with List Comprehension?

后端 未结 8 736
囚心锁ツ
囚心锁ツ 2020-12-29 07:49

List Comprehension for me seems to be like the opaque block of granite that regular expressions are for me. I need pointers.

Say, I have a 2D list:

l         


        
8条回答
  •  抹茶落季
    2020-12-29 08:04

    My favorite, and the shortest one, is this:

    li2 = sum(li, [])
    

    and

    s = ','.join(li2)
    

    EDIT: use sum instead of reduce, (thanks Thomas Wouters!)

提交回复
热议问题