简体   繁体   中英

Using asarray returns TypeError: readable buffer object error

I have a list, which basically looks like:

toLocArray

>>[['Location 1', 'RAILS', 0.10520972, 20000], 
['Location 2', 'ROADS', 0.377652629, 20000], 
['Location 3', 'RAILS', 0.12588421, 20000], 
['Location 4', 'ROADS', 0.377652629, 20000]]

I wish to cast it as an ndarray, with a specified dtype:

dtype = [('toLoc', 'a50'),('Network', 'a10'), ('timeCost', 'f8'),   ('toLocCapacity', 'i4')]
toLocArray = np.asarray(toLocArray, dtype=dtype)

However, I get the following error:

     89     dtype = [('toLoc', 'a50'),('Network', 'a10'), ('timeCost', 'f8'), ('toLocCapacity', 'i4')]
---> 90     toLocArray = np.asarray(toLocArray,dtype=dtype)
     91 
     92 

C:[path]\site-packages\numpy\core\numeric.pyc in asarray(a, dtype, order)
    460 
    461     """
--> 462     return array(a, dtype, copy=False, order=order)
    463 
    464 def asanyarray(a, dtype=None, order=None):

TypeError: expected a readable buffer object 

From what I've read this sort of error occurs when trying to set a value with the wrong type, but I don't see how I could be doing that here. I can get the asarray function to work if I don't use the "dtype=" clause, but I need the fields to be defined. What am I doing wrong?

I found a way to make this work. Don't know if this is the "right" way to do things, but applying a tuple to the original data makes it do what I need:

toLocArray = map(tuple, toLocArray)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM