[Numpy-discussion] Rotate Left and Rotate Right

Cristi Constantin darkgl0w at yahoo.com
Wed May 20 07:53:25 EDT 2009


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.



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090520/874b32f6/attachment.html>


More information about the NumPy-Discussion mailing list