Python reverse-stride slicing

前端 未结 8 614
后悔当初
后悔当初 2020-12-02 20:22

A specific example of my question is, \"How can I get \'3210\' in this example?\"


>>> foo = \'0123456\'
>>> foo[0:4]
\'0123\'
>>> foo[::-1]
\'6543210\'
>>>          


        
相关标签:
8条回答
  • 2020-12-02 21:01
    s="this is my world"
    pattern=re.findall(r'\S+',s)
    a=[]
    for i in range(len(pattern)):
        a.append((pattern[i][::-1]))
    print (a)
    print (" ".join(a))
    
    0 讨论(0)
  • 2020-12-02 21:07

    Simply exclude the end range index...

    >>> foo[3::-1]
    '3210'
    

    Ironically, about the only option I think you didn't try.

    0 讨论(0)
  • 2020-12-02 21:07

    You can use s[::-1] to reverse the entire string. But if you want to reverse each substring with some fixed length, you can first extract the substring and then reverse the entire substring. For example, let's assume we need to check whether each substring with length 3 of string foo is a palindrome, we can do it like this:

    >>> foo = '0102030'
    >>> for i in range(len(foo)-3):
    ...     if foo[i:i+3] == foo[i:i+3][::-1]:
    ...         print(foo[i:i+3], 'is a palindrome')
    ...     else:
    ...         print(foo[i:i+3], 'is not a palindrome')
    ...
    010 is a palindrome
    102 is not a palindrome
    020 is a palindrome
    203 is not a palindrome
    030 is a palindrome
    

    If you want to check if a substring is palindrome like this:

    if foo[i:i+3] == foo[i+2:i-1:-1]:
        ...
    

    you will not be able to handle the case of i being 0, since you are actually comparing foo[0:3] with foo[2:-1:-1], which is equivalent to foo[2:n-1:-1], which in turn is an empty string.

    The only drawback of the first solution is that it uses a little more memory but it's no big deal.

    0 讨论(0)
  • 2020-12-02 21:10

    In addition to the above solutions, you can do something like:

    foo = '0123456'
    foo[-4::-1]
    

    I guess if foo is going to be changing lengths, this may not be the best solution, but if the length is static it would work.

    0 讨论(0)
  • 2020-12-02 21:13

    Given:

    >>> foo = '0123456'
    

    The desired string 3210 is from index 3rd to the 0-th characters:

    >>> stop_idx=0
    >>> start_idx=3
    

    Here are two generic solutions:

    1. Take the forward slice then reverse it:

      >>> foo[stop_idx:start_idx+1][::-1]
      '3210'
      
    2. Based on this answer, use a negative step and stop 1 element before the first element (plus the stop offset):

      >>> foo[start_idx:stop_idx-len(foo)-1:-1]
      '3210'
      
      >>> a[start_idx:stop_idx-len(a)-1:-1]
      [2, 1]
      

    Comparing execution times, the first version is faster:

    >>> timeit.timeit('foo[stop_idx:start_idx+1][::-1]', setup='foo="012345"; stop_idx=0; start_idx=3', number=10_000_000)
    1.7157553750148509
    >>> timeit.timeit('foo[start_idx:stop_idx-len(foo)-1:-1]', setup='foo="012345"; stop_idx=0; start_idx=3', number=10_000_000)
    1.9317215870250948
    
    0 讨论(0)
  • 2020-12-02 21:14

    Omit the end index in your slice notation:

    >>> foo = '0123456'
    >>> foo[3::-1]
    '3210'
    

    If you have to do this many times, create a slice object that you can use over and over

    >>> i = slice(3,None,-1)
    >>> foo[i]
    '3210'
    
    0 讨论(0)
提交回复
热议问题