开发者

How to include the first few items of a numpy array with python

开发者 https://www.devze.com 2023-02-25 00:42 出处:网络
I have array d,I want array d2 The rows do not have the same number of items. d=[ [\'q\', \'u\', \'s\', \'a\', \'p\', \'e\', \'a\']

I have array d, I want array d2 The rows do not have the same number of items.

 d=   [ ['q', 'u', 's', 'a', 'p', 'e', 'a']
     ['500', 'G', 'G', 'C', 'C', 'P', '04/12/2011', '' ]
     ['500', 'G', 'G', 'F', 'C', 'P', '04/12/2011', ''] 
     ['5', 'ZUMZ', 'ZUMZ', 'C', 'C', 'B', '04/12/2011', '']
     ['2', 'ZUMZ', 'ZUMZ', 'F', 'C', 'B', '04/12/2011', '']
     ['7', 'ZUMZ', 'ZUMZ', 'M', 'C', 'B', '04/12/2011', '']]

Only the first five itmes.

 d2=   开发者_如何学C[ ['q', 'u', 's', 'a', 'p']
         ['500', 'G', 'G', 'C', 'C']
         ['500', 'G', 'G', 'F', 'C'] 
         ['5', 'ZUMZ', 'ZUMZ', 'C', 'C']
         ['2', 'ZUMZ', 'ZUMZ', 'F', 'C']
         ['7', 'ZUMZ', 'ZUMZ', 'M', 'C']]


f = urllib.urlopen(url)
f = csv.reader(f)
d= np.asarray(list(f), dtype= 'object')
print d
m=  d[:,:]                
print m   

I tried above and m= d[:,0:5]


How about:

m = np.array([x[:5] for x in d], dtype=object)

Although if they are all strings, you should use a string dtype instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消