Need assistance

MRAB python at mrabarnett.plus.com
Sun Jul 19 12:35:03 EDT 2015


On 2015-07-19 01:59, Denis McMahon wrote:
> On Sat, 18 Jul 2015 12:35:10 +0200, Sibylle Koczian wrote:
>
>> Am 18.07.2015 um 02:40 schrieb Denis McMahon:
>
>>> Having a list of words, get a copy of the list in reverse order. See
>>> the reversed function (and maybe the list function).
>
>> That won't really help, because the desired order is, with the example
>> the OP used: Sirna Daniel Craig. So here indexing is necessary, but
>> indexing of the list elements, not of the characters in the string.
>
> Oh, then it's even easier, yes, it's mainly a case of list indexing.
>
> 1) Split the original string into a list of words (string.split() method)
> 2) create a sublist (s1) of the last element
> 3) create another sublist (s2) of the first to penultimate elements
> 4) combine the two sublists
> 5) use the string.join() method to combine the sublist elements into a
> single string
>
> I think most pythonistas would probably combine steps 2 through 4 in a
> single line of code, possibly even steps 2 through 5.
>
If you use rsplit, you can do it in one line.




More information about the Python-list mailing list