Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Reverse word
#1
Hi!
I need to reverse words like this:
cake
python
Output:
keca onthpy
I used [::-1] but this isn't a correct answer because it gives me:
cake
python
Output:
ekac nohtyp
Any suggestion?
Quote
#2
there is for sure much cleaner way but for now :) :
n = 2    # num of chars 
s = ''.join([line[i:i+n] for i in range(0, len(line), n)][::-1])
with line being your string.
First you create a list out of every n number of characters, then you reverse the list and join the elements into string again.

cake:
Output:
'keca'
python:
Output:
'onthpy'
Quote
#3
You should specify your task more precisely. It's not obvious what are the exact requirements. It seems that there should be two letter chunks from word and they should be in reverse order.

cake -- > ca ke --> ke ca
python --> py th on --> on th py


Do words have always even number of characters?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Life of Brian: Conjugate the verb, "to go" !
Quote
#4
(Jan-28-2019, 09:15 AM)mlieqo Wrote: there is for sure much cleaner way but for now :) :
n = 2    # num of chars 
s = ''.join([line[i:i+n] for i in range(0, len(line), n)][::-1])
with line being your string.
First you create a list out of every n number of characters, then you reverse the list and join the elements into string again.

cake:
Output:
'keca'
python:
Output:
'onthpy'

THANKS! This works!
Quote
#5
There is built-in module textwrap which can be used to get desired results:

>>> from textwrap import wrap
>>> a = 'python'
>>> ''.join(reversed(wrap(a, width=2)))         # [::-1] can be used instead of reversed
onthpy
Just for fun: inefficient mental exercise :

>>> a = 'python'
>>> ''.join([a[a.index(x):a.index(x) + 2] for x in a][::2][::-1])
onthpy
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Life of Brian: Conjugate the verb, "to go" !
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Reverse list items in a stupid way pyth0n123 4 161 Jul-28-2019, 02:52 PM
Last Post: pyth0n123
  is there a reverse index? Skaperen 6 309 Jun-14-2019, 11:47 PM
Last Post: Skaperen
  Score each word from list variable pythonias2019 6 293 Jun-13-2019, 05:44 PM
Last Post: gontajones
  reverse list, incl. nested list Livne_ye 3 218 May-04-2019, 12:34 PM
Last Post: Livne_ye
  Reading one value from an array in reverse DreamingInsanity 10 476 May-04-2019, 08:14 AM
Last Post: DreamingInsanity
  String slicing in python from reverse ift38375 1 196 Apr-29-2019, 06:58 AM
Last Post: perfringo
  Creating a word list opencircles 2 272 Mar-19-2019, 06:00 PM
Last Post: MohanReddy
  Linear search/searching for a word in a file/list kietrichards 3 359 Mar-08-2019, 07:58 PM
Last Post: Larz60+
  Word replace with string dabozz 1 271 Feb-13-2019, 03:11 PM
Last Post: ichabod801
  Extending my text file word count ranker and calculator Drone4four 8 805 Jan-25-2019, 08:25 AM
Last Post: steve_shambles

Forum Jump:


Users browsing this thread: 1 Guest(s)