Login Register






help please filter_list
Author
Message
help please #1
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:

Reply

help please #2
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:

Reply

RE: help please #3
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
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG

Reply

RE: help please #4
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
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG

Reply

RE: help please #5
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????

Reply

RE: help please #6
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????

Reply

RE: help please #7
(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
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG

Reply

RE: help please #8
(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
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG

Reply

RE: help please #9
Time to read the Documentation.

split:
http://docs.python.org/2/library/stdtype...#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.ht...ded-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]
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: help please #10
Time to read the Documentation.

split:
http://docs.python.org/2/library/stdtype...#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.ht...ded-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]
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply