Sinisterly
help please - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Python (https://sinister.ly/Forum-Python)
+--- Thread: help please (/Thread-help-please--48936)

Pages: 1 2


help please - Creed_mybb_import11544 - 04-15-2013

In the contest " Reversing Words and Quick Loop "organized by sir,@Psycho_Coder, i saw these codes posted by @H4R0015K

Code:
data = raw_input("Enter the string : ").split()[::-1] print "Modified String : ", for i in data: print i,

its working perfectly ..
but sorry to say i couldn't understand how this ' split()[::-1] ' works ..
could anyone please explain this to me ..
a lot of thanks in advance

Confusedmiling:


help please - Creed_mybb_import11544 - 04-15-2013

In the contest " Reversing Words and Quick Loop "organized by sir,@Psycho_Coder, i saw these codes posted by @H4R0015K

Code:
data = raw_input("Enter the string : ").split()[::-1] print "Modified String : ", for i in data: print i,

its working perfectly ..
but sorry to say i couldn't understand how this ' split()[::-1] ' works ..
could anyone please explain this to me ..
a lot of thanks in advance

Confusedmiling:


RE: help please - Ex094 - 04-15-2013

For what I've observed this piece of code [::-1] reverses all the characters while .split() removes any spaces in it.

For Example:

Code:
name = 'Ex094' name[::-1] >>>490xE



RE: help please - Ex094 - 04-15-2013

For what I've observed this piece of code [::-1] reverses all the characters while .split() removes any spaces in it.

For Example:

Code:
name = 'Ex094' name[::-1] >>>490xE



RE: help please - Creed_mybb_import11544 - 04-15-2013

yep.. but if u put a big sentence it will reverse the words positions without reversing the letters of the words of the sentence ... but how????


RE: help please - Creed_mybb_import11544 - 04-15-2013

yep.. but if u put a big sentence it will reverse the words positions without reversing the letters of the words of the sentence ... but how????


RE: help please - Ex094 - 04-15-2013

(04-15-2013, 06:51 PM)Creed Wrote: yep.. but if u put a big sentence it will reverse the words positions without reversing the letters of the words of the sentence ... but how????
It does the same with the sentence string, The above example should be understood but If you don't then maybe the OP of that code might explain better


RE: help please - Ex094 - 04-15-2013

(04-15-2013, 06:51 PM)Creed Wrote: yep.. but if u put a big sentence it will reverse the words positions without reversing the letters of the words of the sentence ... but how????
It does the same with the sentence string, The above example should be understood but If you don't then maybe the OP of that code might explain better


RE: help please - Deque - 04-15-2013

Time to read the Documentation.

split:
http://docs.python.org/2/library/stdtypes.html#str.split

Quote:str.split([sep[, maxsplit]])
Return a list of the words in the string, using sep as the delimiter string.
[...]
If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.

Extended Slices:
http://docs.python.org/2/whatsnew/2.3.html#extended-slices

Quote:Ever since Python 1.4, the slicing syntax has supported an optional third “step” or “stride” argument. For example, these are all legal Python syntax: L[1:10:2], L[:-1:1], L[::-1]. This was added to Python at the request of the developers of Numerical Python, which uses the third argument extensively. However, Python’s built-in list, tuple, and string sequence types have never supported this feature, raising a TypeError if you tried it. Michael Hudson contributed a patch to fix this shortcoming.

Negative values also work to make a copy of the same list in reverse order:

Code:
>>> L[::-1] [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]



RE: help please - Deque - 04-15-2013

Time to read the Documentation.

split:
http://docs.python.org/2/library/stdtypes.html#str.split

Quote:str.split([sep[, maxsplit]])
Return a list of the words in the string, using sep as the delimiter string.
[...]
If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.

Extended Slices:
http://docs.python.org/2/whatsnew/2.3.html#extended-slices

Quote:Ever since Python 1.4, the slicing syntax has supported an optional third “step” or “stride” argument. For example, these are all legal Python syntax: L[1:10:2], L[:-1:1], L[::-1]. This was added to Python at the request of the developers of Numerical Python, which uses the third argument extensively. However, Python’s built-in list, tuple, and string sequence types have never supported this feature, raising a TypeError if you tried it. Michael Hudson contributed a patch to fix this shortcoming.

Negative values also work to make a copy of the same list in reverse order:

Code:
>>> L[::-1] [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]