Thread: Python Newbie

Page 2 of 2 First 12
  • Jump to page:
    #16
  1. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Code:
    >>> reverse = []
    >>>
    >>> for index, value in enumerate(someInput):
    ...     reverse.append(someInput[-index - 1])
    ...
    >>> reverse
    ['t', 'u', 'p', 'n', 'i', '_', 'w', 'a', 'r']
    >>> ''.join(reverse)
    'tupni_war'
    >>>
    >>> someInputList = [someInput[-i - 1] for i, v in enumerate(someInput)]
    >>> someInputList
    ['t', 'u', 'p', 'n', 'i', '_', 'w', 'a', 'r']
    >>> ''.join(someInputList)
    'tupni_war'
    >>>
    Don't worry about understanding the second example since chances are you haven't seen list comprehensions before anyway - just know that its a more compact version of the for loop version.

    Mark.
    programming language development: www.netytan.com Hula

  2. #17
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    36
    Rep Power
    10
    Originally Posted by NewPythoner
    Hi Nokio,
    You were right with your code...just a small error

    Code:
    >>> original=raw_input('Enter the string to be reversed........')
    >>> original
    'AIDNI'
    >>> reverse=''
    >>> 
    >>> for i in range(0,len(original)):
    ... 	reverse+=original[(len(original)-i)-1]
    ... 	
    >>> reverse
    'INDIA'
    >>>
    Thats what you want probably....but then using the reverse() function wld be a smarter way of doing it!

    Rgds,
    Subha
    Thx with your code i found out what was the error

    chaine2 = chaine2 + chaine1[(length-1)-i]

    But how come chaine1[length-1-i] would generate an error?

    Can't python do the operation length-1-i in one shot?
  4. #18
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    Now now... there is a goof up here Nokio....your original code also works man! There's no error here

    reverse+=original[(len(original)-i)-1] does the same thing as your chaine2 = chaine2 + chaine1[length-1-i]

    You were right actually...then how come you got an error?

    Rgds,
    Subha
  6. #19
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    36
    Rep Power
    10
    Originally Posted by NewPythoner
    Now now... there is a goof up here Nokio....your original code also works man! There's no error here

    reverse+=original[(len(original)-i)-1] does the same thing as your chaine2 = chaine2 + chaine1[length-1-i]

    You were right actually...then how come you got an error?

    Rgds,
    Subha
    Crap it worked since the beginnin. I swore it has generated an error.... ah well .. i must have dreamed.

    Thx.
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo