Python - Function to reverse strings in LIST

后端 未结 2 932
误落风尘
误落风尘 2021-01-29 06:09

studying Python as crazy and have many many questions.

This time about function, i need to create 2 functions, first for numbers to sum up everything that user inputs in

相关标签:
2条回答
  • 2021-01-29 06:48

    Judging by your question it seems you are learning functions in python, you can reverse the list using a function like this:

    strings_ = [str(x) for x in input("Please input at least 5 words (Use Space): ").split()]
    
    for index,item in enumerate(strings_):
    
        def recursion_reverse(string_1):
            if not string_1:
                return ""
            else:
                front_part=recursion_reverse(string_1[1:])
                back_part=string_1[0]
    
    
    
            return front_part+back_part[0]
    
        strings_[index]=recursion_reverse(item)
    
    print(strings_)
    

    output:

    Please input at least 5 words (Use Space): Hello world this is me
    ['olleH', 'dlrow', 'siht', 'si', 'em']
    
    0 讨论(0)
  • 2021-01-29 06:51

    For your first function, that already exists as a built-in function of same name (sum()). For the second, you can use a simple list comprehension.

    def rever(strings):
        return [x[::-1] for x in strings]
    
    0 讨论(0)
提交回复
热议问题