suchen

Heim  >  Fragen und Antworten  >  Hauptteil

python中怎么倒着遍历字符串?

PHP中文网PHP中文网2806 Tage vor918

Antworte allen(5)Ich werde antworten

  • 怪我咯

    怪我咯2017-04-18 09:31:35

    双端队列

    from collections import deque
    
    
    def string_reverse(string):
        d = deque()
        d.extendleft(string)
        return ''.join(d);
    

    Antwort
    0
  • 阿神

    阿神2017-04-18 09:31:35

    def rev(s):
        r = list()
        for c in s:
            r.insert(0, c)
        return ''.join(r)

    Antwort
    0
  • 大家讲道理

    大家讲道理2017-04-18 09:31:35

    str = "hello world"
    l = len(str)
    for i in range(l):
        print str[l - i - 1]

    Antwort
    0
  • 阿神

    阿神2017-04-18 09:31:35

    def reserveStr(s):
        length = len(s)
        result = ''
        for i in range(length):
            result += s[length - i - 1]
        return result
            
    

    Antwort
    0
  • 巴扎黑

    巴扎黑2017-04-18 09:31:35

    def reversed_index(n):
        for i in range(n-1, -1, -1):
            yield i
    
    
    test_string = "我爱北京天安门"
    new_string = ""
    for i in reversed_index(len(test_string)):
        new_string += test_string[i]
    print(new_string)

    Antwort
    0
  • StornierenAntwort