Turning a list into nested lists in python

后端 未结 8 1896
天涯浪人
天涯浪人 2020-11-30 05:15

Possible Duplicate:
How can I turn a list into an array in python?

How can I turn a list such as:

<         


        
相关标签:
8条回答
  • 2020-11-30 05:48

    Based on the answer from Fred Foo, if you're already using numpy, you may use reshape to get a 2d array without copying the data:

    import numpy
    new_list = numpy.array(data_list).reshape(-1, 3)
    
    0 讨论(0)
  • 2020-11-30 05:49

    new_list = [data_list[x:x+3] for x in range(0, len(data_list) - 2, 3)]

    List comprehensions for the win :)

    0 讨论(0)
提交回复
热议问题