0

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=   [ ['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]

2
  • m= d[:,0:5] should return exactly first five columns of an array. what do you get instead? Commented Apr 14, 2011 at 12:49
  • @user428862 and @Andrey: The issue is that if your list of lists is (N,M) and you use the object dtype, you get an (N,) array, where each element is a list, rather than an array of M elements. Commented Apr 14, 2011 at 13:29

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@user428862: I didn't mean dtype=string, rather a string dtype, which would be something like dtype='S8' or dtype=(str,16), etc.
oh, thanks, would there be a pick in speed, likely more compact, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.