python 删除字符串中重复字符(单词)的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/636977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:31:29  来源:igfitidea点击:

Best way to remove duplicate characters (words) in a string?

pythonstringduplicates

提问by Amything

What would be the best way of removing any duplicate characters and sets of characters separated by spaces in string?

删除字符串中以空格分隔的任何重复字符和字符集的最佳方法是什么?

I think this example explains it better:

我认为这个例子更好地解释了它:

foo = 'h k k h2 h'

should become:

应该变成:

foo = 'h k h2' # order not important

Other example:

其他例子:

foo = 's s k'

becomes:

变成:

foo = 's k'

回答by Brian R. Bondy

' '.join(set(foo.split()))

Note that split() by default will split on all whitespace characters. (e.g. tabs, newlines, spaces)

请注意,默认情况下 split() 将拆分所有空白字符。(例如制表符、换行符、空格)

So if you want to split ONLY on a space then you have to use:

因此,如果您只想在空间上拆分,则必须使用:

' '.join(set(foo.split(' ')))

回答by S.Lott

Do you mean?

你的意思是?

' '.join( set( someString.split() ) )

That's the unique space-delimited words in no particular order.

这是没有特定顺序的唯一以空格分隔的单词。

回答by Matthew Marshall

out = []
for word in input.split():
    if not word in out:
        out.append(word)
output_string = " ".join(out)

Longer than using a set, but it keeps the order.

比使用 set 更长,但它保持顺序。

Edit:Nevermind. I missed the part in the question about order not being important. Using a set is better.

编辑:没关系。我错过了关于订单不重要的问题的部分。使用一套更好。