#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Print 10 charaters from right in python


    Hi,

    I want to print 10 characters from right.

    e.g. name="abcdef123456789asd"
    i should be able to get o/p as "dsa9876543"

    I tried the below method:

    name = "abcdef123456789asd"
    reverse= name[::-1] # This reverses the entire string
    name=reverse[0:10]
    print name

    but believe there should be some other shorter way of doing it.
    Request you to pls help.

    Thanks
    Siva
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Code:
    >>> 'abcdefghijklmnop'[-1:-11:-1]
    'ponmlkjihg'
    >>> len('abcdefghijklmnop'[-1:-11:-1])
    10
    >>> reversed('123')
    <reversed object at 0xd32dd0>
    >>> list(reversed('123'))
    ['3', '2', '1']
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Code:
    >>> 'abcdefghijklmnop'[-1:-11:-1]
    'ponmlkjihg'
    >>> len('abcdefghijklmnop'[-1:-11:-1])
    10
    >>> reversed('123')
    <reversed object at 0xd32dd0>
    >>> list(reversed('123'))
    ['3', '2', '1']
    >>>
    hi b49P23TIvg,

    Thanks for your reply.. it works fine... can you pls explain how does this [-1:-11:-1] calculate to 10.

    Thanks,
    Siva
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    'ponmlkjihg'

    10 == len( 'ponmlkjihg' )
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by b49P23TIvg
    'ponmlkjihg'

    10 == len( 'ponmlkjihg' )

    I understood it.. Thanks.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Printing characters


    I am new to python and trying out different things

    a="abcdefghijklmnopqrst"

    I want to print the last 10 characters i.e. klmnopqrst .. How do i do this?

    Please let me know. Also , request you to suggest a good book for understanding python

    Thanks
    Siva
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Code:
    >>> #please try lots of slice tests on your own in the python interpreter
    >>> A=list(range(20))
    >>> A[1::2]  # odd
    >>> A[::2]  # objects at even indexes
    >>> A[:10]  # first 10 objects
    >>> A[10:14] # objects 10, 11, 12, and 13
    www.python.org find the tutorial. Read it and experiment in the python interpreter as you go.
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Code:
    >>> #please try lots of slice tests on your own in the python interpreter
    >>> A=list(range(20))
    >>> A[1::2]  # odd
    >>> A[::2]  # objects at even indexes
    >>> A[:10]  # first 10 objects
    >>> A[10:14] # objects 10, 11, 12, and 13
    www.python.org find the tutorial. Read it and experiment in the python interpreter as you go.

    Thanks mate!!!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    LIST - search


    Can you try this?
    I have a list that says

    Now I want to loop thru this and compare a value in a variable with the value of num in the above list. Any ideas??

    Like my var may have 004, I need to check this against 'num' in the above list....

    Thanks for the reply.... I think it should be

    [('key1=1,t=US', {'Num': ['004'],'name': ['Roger'],'locCity': ['Oakbrook']}),
    ('key1=2,t=US', {'Num': ['005'],'name': ['Din'],'locCity': ['Brookside']})
    ]

    Yes...Hope it makes sense now....
    Thanks.
  18. #10
  19. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    You posted a syntax error. (Bad quotes, in my opinion.) What do you mean?
    [code]Code tags[/code] are essential for python code and Makefiles!
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Unhappy


    That's correct...I have reposted.
  22. #12
  23. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Code:
    >>> var = '004'
    >>> import pprint
    >>> pprint.pprint(A)
    [('key1=1,t=US', {'Num': ['004'], 'locCity': ['Oakbrook'], 'name': ['Roger']}),
     ('key1=2,t=US', {'Num': ['005'], 'locCity': ['Brookside'], 'name': ['Din']})]
    >>> for L in A:
    ...  D = L[1]
    ...  for value in D.values():
    ...   if var in value:
    ...    print('found')
    >>>
    When structures get convoluted so that you need specialized functions to extract data you might do better with a class.
    [code]Code tags[/code] are essential for python code and Makefiles!
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0
    Thanks very much. Appreciate your time.
    Will try this code. Thanks...
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0
    From the following LIST, I need to create a LIST or DICTIONARY where I need few fields from each row based on a match. Like I will get only 1 match at a time.

    [('key1=1,t=US',
    {'Num': ['004'], 'locCity': 'Oakbrook'], 'name': ['Roger'],'uid': [lname.fname@msn.com],'Date': [101010]}),
    ('key1=2,t=US',
    {'Num': ['005'], 'locCity': ['Brookside'], 'name': ['Din'],'uid':[lname1.fname1@msn.com],'Date': [101110]}),
    ('key1=3,t=US',
    {'Num': ['006'], 'locCity': ['OakBrook'], 'name': ['Dan'],'uid':[lname2.fname2@msn.com],'Date': [111010]})]

    I have a string with lastname.firstname. With this I should loop thru the above tuples and for the matching entry, I have to store keys like Num, locCity and Date and their values. So I believe it should be a dictionary?.
    Could it be done? Please advise.
  28. #15
  29. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    You ignored my "design a class for complicated nests of lists and dictionaries" advice. Perhaps start a new thread to get someone else's opinion.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo