python去除重复单词_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

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值