On Wed, May 20, 2009 at 7:53 AM, Cristi Constantin <darkg...@yahoo.com> wrote: > Good day, me again. > I have this string of data : > String = 'i want\nto go\nto the\nbeach'. > > I want to rotate this data to left, or to right, after is split it after > '\n'. > Note that it's important to use 'U' array, because i might have unicode > characters in this string. > > So normal, the text is: > i want > to go > to the > beach > > Rotated right would be: > btti > eoo > a w > ctga > hhon > e t > > Rotated left would be: > t e > nohh > agtc > w a > ooe > ittb > > There are a few methods i guess could be used. > > Split like: [np.array([j for j in i],'U') for i in String.split('\n')] => > [ array([u'i', u' ', u'w', u'a', u'n', u't'], dtype='<U1'), > array([u't', u'o', u' ', u'g', u'o'], dtype='<U1'), > array([u't', u'o', u' ', u't', u'h', u'e'], dtype='<U1'), > array([u'b', u'e', u'a', u'c', u'h'], dtype='<U1') ] > > Or split like: np.array( String.split('\n'), 'U' ), => array([u'i want', > u'to go', u'to the', u'beach'], dtype='<U6') > Both methods are impossible to use with my knowledge. > > Without Numpy, if i want to rotate to right: i should reverse a list of > splited by '\n' strings, save max length for all lines, align all lines to > max len, rotate the square with map( lambda *row: [elem for elem in row], > *Content ), then join the resulted sub-lists, unite with '\n' and return. > > I need to know if there is a faster Numpy approach... maybe explode the > string by '\n' and rotate fast, or something? > > Thank you very much, in advance. >
If you have many lines of different length, then using sparse matrices might be useful. scipy\maxentropy\examples might be useful to look at. I don't know much about language processing but the following seems to work, using 1 character arrays: Josef >>> ss = u'i want\nto go\nto the\nbeach' >>> lines = ss.split('\n') >>> max(len(line) for line in lines) 6 >>> maxl = max(len(line) for line in lines) >>> uarr = np.zeros((len(lines),maxl),dtype='<U1') >>> uarr array([[u'', u'', u'', u'', u'', u''], [u'', u'', u'', u'', u'', u''], [u'', u'', u'', u'', u'', u''], [u'', u'', u'', u'', u'', u'']], dtype='<U1') >>> for i,line in enumerate(lines): uarr[i,:len(line)] = [j for j in line] >>> uarr array([[u'i', u' ', u'w', u'a', u'n', u't'], [u't', u'o', u' ', u'g', u'o', u''], [u't', u'o', u' ', u't', u'h', u'e'], [u'b', u'e', u'a', u'c', u'h', u'']], dtype='<U1') >>> uarr[::-1,::-1] array([[u'', u'h', u'c', u'a', u'e', u'b'], [u'e', u'h', u't', u' ', u'o', u't'], [u'', u'o', u'g', u' ', u'o', u't'], [u't', u'n', u'a', u'w', u' ', u'i']], dtype='<U1') >>> uarr[:,::-1].T # rotate left ? array([[u't', u'', u'e', u''], [u'n', u'o', u'h', u'h'], [u'a', u'g', u't', u'c'], [u'w', u' ', u' ', u'a'], [u' ', u'o', u'o', u'e'], [u'i', u't', u't', u'b']], dtype='<U1') >>> uarr[::-1,:].T # rotate right ? array([[u'b', u't', u't', u'i'], [u'e', u'o', u'o', u' '], [u'a', u' ', u' ', u'w'], [u'c', u't', u'g', u'a'], [u'h', u'h', u'o', u'n'], [u'', u'e', u'', u't']], dtype='<U1') _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion