Hướng dẫn how do you reverse a sentence in python? - làm thế nào để bạn đảo ngược một câu trong python?

Định nghĩa vấn đề

Tạo một chương trình Python để đảo ngược một câu.

Thuật toán

  1. Lấy một chuỗi làm đầu vào.
  2. Chuyển đổi câu thành một danh sách các từ.
  3. Tham gia danh sách theo thứ tự ngược lại mà cuối cùng là câu đảo ngược.

Chương trình

sentence = "dread it run from it destiny still arrives"
word_list = sentence.split()
reversed_list = word_list[:: -1]
reversed_sentence = " ".join(reversed_list)
print(reversed_sentence)

Đầu ra

arrives still destiny it from run it dread

Chương trình này có thể được nén thêm.

sentence = "dread it run from it destiny still arrives"
print(" ".join(sentence.split()[::-1]))

Đầu ra

arrives still destiny it from run it dread

Chương trình này có thể được nén thêm.

sentence = "dread it run from it destiny still arrives"
word_list = sentence.split()
reversed_list = reversed(word_list)
reversed_sentence = " ".join(reversed_list)
print(reversed_sentence)

Danh sách Python có thể được đảo ngược bằng phương pháp arrives still destiny it from run it dread2, có thể được sử dụng thay cho arrives still destiny it from run it dread3 trong chương trình như sau.

sentence = input("Enter a sentence :")
print(" ".join(reversed(sentence.split())))

Đầu ra

Enter a sentence :This is an input
input an is This

Chương trình này có thể được nén thêm.

Giải pháp này sử dụng cùng một quy trình nhưng có các phương thức khác nhau để đảo ngược các từ trong chuỗi với phép lặp trợ giúp ngược và mô -đun biểu thức chính quy. Sau đây là các bước của cách tiếp cận của chúng tôi:

Tìm tất cả các từ trong chuỗi với chức năng trợ giúp của hàm re.findall (). & Nbsp;

Lặp qua danh sách theo cách lạc hậu. & Nbsp;

Examples:

Input : str =" geeks quiz practice code"
Output : str = code practice quiz geeks  
Input : str = "my name is laxmi"
output : str= laxmi is name my 

Tham gia các từ trong một chuỗi với hàm trợ giúp của tham gia ().

Python3

Enter a sentence :This is an input
input an is This
9
Input : str =" geeks quiz practice code"
Output : str = code practice quiz geeks  
Input : str = "my name is laxmi"
output : str= laxmi is name my 
0

arrives still destiny it from run it dread
6
arrives still destiny it from run it dread
7

Các

arrives still destiny it from run it dread
0
sentence = "dread it run from it destiny still arrives"
word_list = sentence.split()
reversed_list = reversed(word_list)
reversed_sentence = " ".join(reversed_list)
print(reversed_sentence)
5
arrives still destiny it from run it dread
5
sentence = "dread it run from it destiny still arrives"
word_list = sentence.split()
reversed_list = reversed(word_list)
reversed_sentence = " ".join(reversed_list)
print(reversed_sentence)
2
code practice quiz geeks
3__

arrives still destiny it from run it dread
0
sentence = input("Enter a sentence :")
print(" ".join(reversed(sentence.split())))
2
sentence = input("Enter a sentence :")
print(" ".join(reversed(sentence.split())))
3

arrives still destiny it from run it dread
2
arrives still destiny it from run it dread
3
arrives still destiny it from run it dread
4
arrives still destiny it from run it dread
5

Đầu ra

code practice quiz geeks

Độ phức tạp về thời gian: O (n), trong đó n là độ dài của không gian chuỗi chuỗi O(n), where n is the length of the string
Auxiliary Space: O(n), where n is the length of the string

Chúng ta được cung cấp một chuỗi và chúng ta cần đảo ngược các từ của một chuỗi đã cho? Ví dụ:

Input : str = geeks quiz practice code
Output : str = code practice quiz geeks

Vấn đề này có một giải pháp hiện có, vui lòng giới thiệu các từ ngược trong một liên kết chuỗi đã cho. Chúng tôi sẽ giải quyết vấn đề này trong Python. Đưa ra dưới đây là các bước cần tuân thủ để giải quyết vấn đề này.

  • Tách từng từ trong một chuỗi đã cho bằng phương thức Split () của Kiểu dữ liệu chuỗi trong Python.
  • Đảo ngược danh sách phân tách từ.
  • In các từ của danh sách, ở dạng chuỗi sau khi nối từng từ với không gian bằng phương thức.

Implementation:

Python3

arrives still destiny it from run it dread
6
arrives still destiny it from run it dread
7

arrives still destiny it from run it dread
0
arrives still destiny it from run it dread
9
arrives still destiny it from run it dread
5
sentence = "dread it run from it destiny still arrives"
word_list = sentence.split()
reversed_list = reversed(word_list)
reversed_sentence = " ".join(reversed_list)
print(reversed_sentence)
1
sentence = "dread it run from it destiny still arrives"
word_list = sentence.split()
reversed_list = reversed(word_list)
reversed_sentence = " ".join(reversed_list)
print(reversed_sentence)
2
sentence = "dread it run from it destiny still arrives"
word_list = sentence.split()
reversed_list = reversed(word_list)
reversed_sentence = " ".join(reversed_list)
print(reversed_sentence)
3

arrives still destiny it from run it dread
0
sentence = "dread it run from it destiny still arrives"
word_list = sentence.split()
reversed_list = reversed(word_list)
reversed_sentence = " ".join(reversed_list)
print(reversed_sentence)
5
arrives still destiny it from run it dread
5
sentence = "dread it run from it destiny still arrives"
word_list = sentence.split()
reversed_list = reversed(word_list)
reversed_sentence = " ".join(reversed_list)
print(reversed_sentence)
2
sentence = "dread it run from it destiny still arrives"
word_list = sentence.split()
reversed_list = reversed(word_list)
reversed_sentence = " ".join(reversed_list)
print(reversed_sentence)
8
sentence = "dread it run from it destiny still arrives"
word_list = sentence.split()
reversed_list = reversed(word_list)
reversed_sentence = " ".join(reversed_list)
print(reversed_sentence)
9
sentence = input("Enter a sentence :")
print(" ".join(reversed(sentence.split())))
0

arrives still destiny it from run it dread
0
sentence = input("Enter a sentence :")
print(" ".join(reversed(sentence.split())))
2
sentence = input("Enter a sentence :")
print(" ".join(reversed(sentence.split())))
3

sentence = input("Enter a sentence :")
print(" ".join(reversed(sentence.split())))
4
sentence = input("Enter a sentence :")
print(" ".join(reversed(sentence.split())))
5
arrives still destiny it from run it dread
5
arrives still destiny it from run it dread
5
sentence = input("Enter a sentence :")
print(" ".join(reversed(sentence.split())))
8
sentence = input("Enter a sentence :")
print(" ".join(reversed(sentence.split())))
9

arrives still destiny it from run it dread
0
Enter a sentence :This is an input
input an is This
1
arrives still destiny it from run it dread
5
Enter a sentence :This is an input
input an is This
3

arrives still destiny it from run it dread
0
arrives still destiny it from run it dread
2
Enter a sentence :This is an input
input an is This
6
Enter a sentence :This is an input
input an is This
1
Enter a sentence :This is an input
input an is This
8

Đầu ra

code practice quiz geeks

Độ phức tạp về thời gian: O (n), trong đó n là độ dài của không gian chuỗi chuỗi O(n), where n is the length of the string
Auxiliary Space: O(n), where n is the length of the string

Chúng ta được cung cấp một chuỗi và chúng ta cần đảo ngược các từ của một chuỗi đã cho? Ví dụ:

  • Vấn đề này có một giải pháp hiện có, vui lòng giới thiệu các từ ngược trong một liên kết chuỗi đã cho. Chúng tôi sẽ giải quyết vấn đề này trong Python. Đưa ra dưới đây là các bước cần tuân thủ để giải quyết vấn đề này.
  • Tách từng từ trong một chuỗi đã cho bằng phương thức Split () của Kiểu dữ liệu chuỗi trong Python.
  • Đảo ngược danh sách phân tách từ.

Python3

In các từ của danh sách, ở dạng chuỗi sau khi nối từng từ với không gian bằng phương thức.

arrives still destiny it from run it dread
6
arrives still destiny it from run it dread
7

arrives still destiny it from run it dread
0
arrives still destiny it from run it dread
9
arrives still destiny it from run it dread
5
sentence = "dread it run from it destiny still arrives"
word_list = sentence.split()
reversed_list = reversed(word_list)
reversed_sentence = " ".join(reversed_list)
print(reversed_sentence)
1
sentence = "dread it run from it destiny still arrives"
word_list = sentence.split()
reversed_list = reversed(word_list)
reversed_sentence = " ".join(reversed_list)
print(reversed_sentence)
2
sentence = "dread it run from it destiny still arrives"
word_list = sentence.split()
reversed_list = reversed(word_list)
reversed_sentence = " ".join(reversed_list)
print(reversed_sentence)
3

arrives still destiny it from run it dread
0
sentence = "dread it run from it destiny still arrives"
word_list = sentence.split()
reversed_list = reversed(word_list)
reversed_sentence = " ".join(reversed_list)
print(reversed_sentence)
5
arrives still destiny it from run it dread
5
sentence = "dread it run from it destiny still arrives"
word_list = sentence.split()
reversed_list = reversed(word_list)
reversed_sentence = " ".join(reversed_list)
print(reversed_sentence)
2
sentence = "dread it run from it destiny still arrives"
word_list = sentence.split()
reversed_list = reversed(word_list)
reversed_sentence = " ".join(reversed_list)
print(reversed_sentence)
8
sentence = "dread it run from it destiny still arrives"
word_list = sentence.split()
reversed_list = reversed(word_list)
reversed_sentence = " ".join(reversed_list)
print(reversed_sentence)
9
sentence = input("Enter a sentence :")
print(" ".join(reversed(sentence.split())))
0

arrives still destiny it from run it dread
0
sentence = input("Enter a sentence :")
print(" ".join(reversed(sentence.split())))
2
sentence = input("Enter a sentence :")
print(" ".join(reversed(sentence.split())))
3

sentence = input("Enter a sentence :")
print(" ".join(reversed(sentence.split())))
4
sentence = input("Enter a sentence :")
print(" ".join(reversed(sentence.split())))
5
arrives still destiny it from run it dread
5
arrives still destiny it from run it dread
5
sentence = input("Enter a sentence :")
print(" ".join(reversed(sentence.split())))
8
sentence = input("Enter a sentence :")
print(" ".join(reversed(sentence.split())))
9

arrives still destiny it from run it dread
0
Enter a sentence :This is an input
input an is This
1
arrives still destiny it from run it dread
5
Enter a sentence :This is an input
input an is This
3

arrives still destiny it from run it dread
0
arrives still destiny it from run it dread
2
Enter a sentence :This is an input
input an is This
6
Enter a sentence :This is an input
input an is This
1
Enter a sentence :This is an input
input an is This
8

Đầu ra

code practice quiz geeks

Giải pháp này sử dụng cùng một quy trình nhưng có các phương thức khác nhau để đảo ngược các từ trong chuỗi với phép lặp trợ giúp ngược và mô -đun biểu thức chính quy. Sau đây là các bước của cách tiếp cận của chúng tôi:Shashank Mishra (Gullu). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.