如何使用Python删除字符串中的重复单词? [英] How can I remove duplicate words in a string with Python?

查看:1718
本文介绍了如何使用Python删除字符串中的重复单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下示例:

string1 = "calvin klein design dress calvin klein"

如何删除第二个两个副本calvin klein

How can I remove the second two duplicates "calvin" and "klein"?

结果应该像

string2 = "calvin klein design dress"

只有第二个副本应该被删除单词的顺序不应该改变!

only the second duplicates should be removed and the sequence of the words should not be changed!

推荐答案

def unique_list(l):
    ulist = []
    [ulist.append(x) for x in l if x not in ulist]
    return ulist

a="calvin klein design dress calvin klein"
a=' '.join(unique_list(a.split()))

这篇关于如何使用Python删除字符串中的重复单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆