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

后端 未结 8 737
囚心锁ツ
囚心锁ツ 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:05

    There are many ways to do this problem. I like Numpy's tools because it is normally already imported in everything I do. However, if you aren't using Numpy for anything else this probably isn't a good method.

    import numpy
    li = [[0,1,2],[3,4,5],[6,7,8]]
    li2=li[0] #first element of array to merge
    i=1 
    while i

    This would print [0 1 2 3 4 5 6 7 8] and then you can convert this into your string too.

提交回复
热议问题