Difference between list(numpy_array) and numpy_array.tolist()

后端 未结 3 1313
误落风尘
误落风尘 2021-01-04 21:07

What is the difference between applying list() on a numpy array vs. calling tolist()?

I was checking the types of both output

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-04 21:47

    .tolist() appears to convert all of the values recursively to python primitives (list), whereas list creates a python list from an iterable. Since the numpy array is an array of arrays, list(...) creates a list of arrays

    You can think of list as a function that looks like this:

    # Not the actually implementation, just for demo purposes
    def  list(iterable):
        newlist = []
        for obj in iter(iterable):
            newlist.append(obj)
        return newlist
    

提交回复
热议问题