[Python] how to use collections.deque to reverse the words in the following sentence. The big...

Discussão em 'Python' iniciado por Stack, Março 6, 2025.

  1. Stack

    Stack Membro Participativo

    from collections import deque

    # Original sentence
    sentence = "The big brown fox jumps over the lazy dog."

    # Step 1: Split the sentence into words
    words = sentence.split()

    # Step 2: Create a deque from the list of words
    word_deque = deque(words)

    # Step 3: Reverse the deque
    word_deque.reverse()

    # Step 4: Join the reversed words back into a single string
    reversed_sentence = ' '.join(word_deque)

    # Print the result
    print(reversed_sentence)

    Continue reading...

Compartilhe esta Página