def revers_e[str_one,str_two]:
for i in range[len[str_one]]:
for j in range[len[str_two]]:
try:
if str_one[i] == str_two[j]:
first_part=str_one[0:i]
second_part=str_one[i+1:]
str_one =first_part+second_part
print[str_one]
else:
print['There is no relation']
except IndexError:
return
str_one = input['Put your First String: ']
str_two = input['Put your Second String: ']
revers_e[str_one, str_two]
Tôi đã sửa đổi mã của bạn, lấy ra một vài bit và thêm một vài bit nữa.
str_one = input['Put your First String: '].split[]
Tôi đã loại bỏ .split[]
, bởi vì tất cả những điều này sẽ làm là tạo một danh sách độ dài 1, vì vậy trong vòng lặp của bạn, bạn sẽ so sánh toàn bộ chuỗi của chuỗi thứ nhất với một chữ cái của chuỗi thứ hai.
str_one = [str_one - str_one[i]].split[]
Bạn không thể xóa một ký tự khỏi một chuỗi như thế này trong Python, vì vậy tôi chia chuỗi thành các phần [bạn cũng có thể chuyển đổi chúng thành các danh sách như tôi đã làm trong mã khác mà tôi đã xóa] theo đó tất cả các ký tự cho đến ký tự cuối cùng Trước khi ký tự phù hợp được bao gồm, theo sau là tất cả các ký tự sau ký tự phù hợp, sau đó được gắn vào một chuỗi.
Tôi đã sử dụng các câu lệnh ngoại lệ, vì vòng lặp đầu tiên sẽ sử dụng độ dài ban đầu, nhưng điều này có thể thay đổi, vì vậy có thể dẫn đến lỗi.
Cuối cùng, tôi chỉ gọi chức năng thay vì in nó, bởi vì tất cả những gì làm là trả về một loại None
.
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
ĐọcS1 and S2, representing sentences, the task is to print both sentences after removing all words which are present in both sentences.
Bàn luận S1 = “sky is blue in color”, S2 =”Raj likes sky blue color “
Output: is in
Raj likes
Explanation: The common words are [ sky, blue, color ]. Removing these words from the two sentences modifies the sentences to the specified output.Với hai chuỗi S1 và S2, đại diện cho các câu, nhiệm vụ là in cả hai câu sau khi xóa tất cả các từ có trong cả hai câu. S1 = “learn data structures and algorithms in GeeksforGeeks“, S2 = “GeeksforGeeks is the computer science portal for Geeks“
Output: learn data structures and algorithms in
is the computer science portal for.
Đầu vào: S1 = Sky Sky có màu xanh da trời, S2 = Ra Raj thích màu xanh da trời đầu ra: IS in & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Raj thích & nbsp; giải thích: Các từ phổ biến là [bầu trời, màu xanh, màu]. Loại bỏ các từ này khỏi hai câu sửa đổi các câu thành đầu ra được chỉ định.{IDE} first, before moving on to the solution.
Đầu vào: S1 = Tìm hiểu các cấu trúc và thuật toán dữ liệu trong GeekSforGeeks, S2 = Hồi GeekSforGeeks là cổng thông tin khoa học máy tính cho đầu ra của Geeks: Tìm hiểu cấu trúc dữ liệu và thuật toán trong & NBSP; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; là cổng thông tin khoa học máy tính cho. Hashing: The problem can be solved using Counter[] function. Follow the steps below to solve the problem:
- Được đề xuất: Vui lòng thử cách tiếp cận của bạn trên {IDE} trước, trước khi chuyển sang giải pháp. split[] and store them in a List.
- Cách tiếp cận bằng cách sử dụng: vấn đề có thể được giải quyết bằng cách sử dụng hàm bộ đếm []. Thực hiện theo các bước dưới đây để giải quyết vấn đề:sentence1 and sentence2, to store the words of the two given sentences.
- Vì tất cả các từ trong một câu được phân tách bằng khoảng trắng, hãy phân chia các từ bằng khoảng trắng bằng cách sử dụng split [] và lưu trữ chúng trong một danh sách.Counter[] function and store it in dictionaries frequency1 and frequency2.
- Khởi tạo hai danh sách, giả sử câu1 và câu2, để lưu trữ các từ của hai câu đã cho.sentence1 and remove the words which are present in the dictionary frequency2.
- Tần số đếm của các từ của cả hai câu bằng hàm bộ đếm [] và lưu trữ nó trong từ điển tần số1 và tần số2.sentence2 and remove the words which are present in the dictionary frequency1.
- Đi qua câu danh sách1 và xóa các từ có trong tần số từ điển2.
Đi qua danh sách câu2 và xóa các từ có trong tần số từ điển1.
Python3
In cả hai danh sách.
Dưới đây là việc thực hiện phương pháp trên:
from
collections
import
Counter
def
str_one = input['Put your First String: '].split[]
0str_one = input['Put your First String: '].split[]
1str_one = input['Put your First String: '].split[]
2str_one = input['Put your First String: '].split[]
3 str_one = input['Put your First String: '].split[]
4str_one = input['Put your First String: '].split[]
5str_one = input['Put your First String: '].split[]
1str_one = input['Put your First String: '].split[]
7str_one = input['Put your First String: '].split[]
3 str_one = input['Put your First String: '].split[]
4 str_one = [str_one - str_one[i]].split[]
0str_one = input['Put your First String: '].split[]
1 str_one = [str_one - str_one[i]].split[]
22str_one = input['Put your First String: '].split[]
3 str_one = [str_one - str_one[i]].split[]
4str_one = input['Put your First String: '].split[]
1 str_one = [str_one - str_one[i]].split[]
6str_one = input['Put your First String: '].split[]
3 str_one = [str_one - str_one[i]].split[]
8str_one = input['Put your First String: '].split[]
1.split[]
0str_one = input['Put your First String: '].split[]
3 .split[]
2None
6None
7
str_one = input['Put your First String: '].split[]
1.split[]
4 .split[]
5.split[]
6 .split[]
7.split[]
8.split[]
9None
0None
1None
2 None
3.split[]
6 None
5
str_one = input['Put your First String: '].split[]
1 str_one = [str_one - str_one[i]].split[]
22str_one = input['Put your First String: '].split[]
3 str_one = [str_one - str_one[i]].split[]
4str_one = input['Put your First String: '].split[]
1 str_one = [str_one - str_one[i]].split[]
6str_one = input['Put your First String: '].split[]
3 str_one = [str_one - str_one[i]].split[]
8str_one = input['Put your First String: '].split[]
1.split[]
0str_one = input['Put your First String: '].split[]
3 .split[]
2str_one = input['Put your First String: '].split[]
1.split[]
4 .split[]
5.split[]
6 .split[]
7.split[]
8.split[]
9None
0str_one = input['Put your First String: '].split[]
1.split[]
4 .split[]
5.split[]
6 .split[]
7.split[]
8.split[]
9None
0None
1None
2 None
3.split[]
6 None
5
str_one = input['Put your First String: '].split[]
1def
0.split[]
8def
2def
3str_one = input['Put your First String: '].split[]
1def
0.split[]
8def
2def
8None
6.split[]
03013 from
1from
22
None
1.split[]
0from
6
str_one = input['Put your First String: '].split[]
3 from
3str_one = input['Put your First String: '].split[]
05
1str_one = input['Put your First String: '].split[]
.split[]
4 .split[]
5.split[]
6 .split[]
7.split[]
8.split[]
9import
0 O[[max[N, M]]2]
Auxiliary Space: O[max[N, M]]
None
1None
2 import
3.split[]
6 import
5Sets and Lists: Follow the steps below to solve the problem:
- Được đề xuất: Vui lòng thử cách tiếp cận của bạn trên {IDE} trước, trước khi chuyển sang giải pháp. split[] and store them in a List.
- Cách tiếp cận bằng cách sử dụng: vấn đề có thể được giải quyết bằng cách sử dụng hàm bộ đếm []. Thực hiện theo các bước dưới đây để giải quyết vấn đề:sentence1 and sentence2, to store the words of the two given sentences.
- Vì tất cả các từ trong một câu được phân tách bằng khoảng trắng, hãy phân chia các từ bằng khoảng trắng bằng cách sử dụng split [] và lưu trữ chúng trong một danh sách.sen1 and sen2.
- Khởi tạo hai danh sách, giả sử câu1 và câu2, để lưu trữ các từ của hai câu đã cho.set intersection of two sets, to store words that are common in both the sentences, say common.
- Tần số đếm của các từ của cả hai câu bằng hàm bộ đếm [] và lưu trữ nó trong từ điển tần số1 và tần số2.sentence1 and pop all the words which are present in the set intersection of two sentences.
- Đi qua câu danh sách1 và xóa các từ có trong tần số từ điển2.
- Đi qua danh sách câu2 và xóa các từ có trong tần số từ điển1.Lists.
Đi qua danh sách câu2 và xóa các từ có trong tần số từ điển1.
Python3
In cả hai danh sách.
Dưới đây là việc thực hiện phương pháp trên:
from
collections
import
Counter
def
str_one = input['Put your First String: '].split[]
0str_one = input['Put your First String: '].split[]
1str_one = input['Put your First String: '].split[]
2str_one = input['Put your First String: '].split[]
3 str_one = input['Put your First String: '].split[]
4str_one = input['Put your First String: '].split[]
5Dưới đây là việc thực hiện phương pháp trên:
from
collections
import
Counter
def
str_one = input['Put your First String: '].split[]
0str_one = input['Put your First String: '].split[]
1str_one = input['Put your First String: '].split[]
2str_one = input['Put your First String: '].split[]
3 str_one = input['Put your First String: '].split[]
4str_one = input['Put your First String: '].split[]
5str_one = input['Put your First String: '].split[]
42def
8str_one = input['Put your First String: '].split[]
1 str_one = [str_one - str_one[i]].split[]
22str_one = input['Put your First String: '].split[]
3 str_one = [str_one - str_one[i]].split[]
4str_one = input['Put your First String: '].split[]
1 str_one = [str_one - str_one[i]].split[]
6str_one = input['Put your First String: '].split[]
3 str_one = [str_one - str_one[i]].split[]
8str_one = input['Put your First String: '].split[]
1.split[]
0str_one = input['Put your First String: '].split[]
3 .split[]
2None
6None
7
str_one = input['Put your First String: '].split[]
1.split[]
4 .split[]
5.split[]
6 .split[]
7.split[]
8.split[]
9None
0None
1None
2 None
3.split[]
6 None
5
str_one = input['Put your First String: '].split[]
1 str_one = [str_one - str_one[i]].split[]
22str_one = input['Put your First String: '].split[]
3 str_one = [str_one - str_one[i]].split[]
4str_one = input['Put your First String: '].split[]
1 str_one = [str_one - str_one[i]].split[]
6str_one = input['Put your First String: '].split[]
3 str_one = [str_one - str_one[i]].split[]
8None
1None
2 import
3.split[]
6
str_one = input['Put your First String: '].split[]
60None
6import
7
None
6.split[]
03013 from
1from
22
None
1.split[]
0from
6
str_one = input['Put your First String: '].split[]
3 from
3str_one = input['Put your First String: '].split[]
1def
0.split[]
8def
2def
3str_one = input['Put your First String: '].split[]
1def
0.split[]
8def
2def
8 str_one = [str_one - str_one[i]].split[]
14str_one = input['Put your First String: '].split[]
3 str_one = input['Put your First String: '].split[]
01 str_one = [str_one - str_one[i]].split[]
17str_one = input['Put your First String: '].split[]
3 str_one = [str_one - str_one[i]].split[]
19 str_one = [str_one - str_one[i]].split[]
20
Độ phức tạp về thời gian: O [Max [N, M]] Không gian phụ trợ: O [Max [N, M]] O[max[N, M]]
Auxiliary Space: O[max[N, M]]