views:

2138

answers:

3

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'

etc.

+7  A: 

Do you mean?

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

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

S.Lott
And ' '.join(set(someString.split())) puts it back together again
David Zaslavsky
+1 but S.Lott, why do you hate pep8 ;D ?
Ali A
@Ali A: 30 years of coding. 20+ years before PEP 8. Can't teach an old fart new trix.
S.Lott
Thanks a lot! Quite the change from what I had.
Amything
@Amything: hard for us to help with new concepts when you don't post your code.
S.Lott
S.Lott: blimey, you sound even older than me!
Ali A
@Ali A: I am old -- my first programming was on an IBM 1620 -- discrete logic, core memory, special room cooling, punched cards, etc., etc.
S.Lott
A: 
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.

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

Matthew Marshall
+5  A: 
' '.join(set(foo.split()))

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

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

' '.join(set(foo.split(' ')))
Brian R. Bondy
' '.join(set(foo.split())) is more pythonic these days. Also, you're missing a closing parenthesis.
John Fouhy
Thanks John, I updated my answer
Brian R. Bondy
+1 the thing about spaces
Ali A
Greatly appreciated!
Amything