Python程序,用于删除给定句子中的所有重复单词。
给一个句子。从给定的句子中删除所有重复的单词。
示例
Input: I am a peaceful soul and blissful soul. Output: I am a peaceful soul and blissful.
算法
Step 1: Split input sentence separated by space into words. Step 2: So to get all those strings together first we will join each string in a given list of strings. Step 3: now create a dictionary using the counter method which will have strings as key and their Frequencies as value. Step 4: Join each words are unique to form single string.
范例程式码
from collections import Counter
def remov_duplicates(st):
st = st.split(" ")
for i in range(0, len(st)):
st[i] = "".join(st[i])
dupli = Counter(st)
s = " ".join(dupli.keys())
print ("After removing the sentence is ::>",s)
# Driver program
if __name__ == "__main__":
st = input("Enter the sentence")
remov_duplicates(st)输出结果
Enter the sentence ::> i am a peaceful soul and blissful soul After removing the sentence is ::> i am a peaceful soul and blissful
热门推荐
10 宝宝祝福语诗意简短 古文
11 邻居女儿嫁人祝福语简短
12 送给同桌的祝福语简短
13 鼓励孩子小考祝福语简短
14 周末语录生日祝福语简短
15 学生祝福语简短古诗句
16 五二零朋友祝福语大全简短
17 对上司的简短祝福语
18 老板生日祝福语简短语