-1

a = aaabbbbccccdefklmopqwwxxxxxyyyyy My question would be, what kind of code should I write in Python in order to: b =abcdefklmopqwxy

so all characters should be appear only once. I am totally newbiew, any answer would be approriated.

baduker
  • 19,152
  • 9
  • 33
  • 56
Viktor
  • 7
  • 3
  • 1
    Is order important? Should the new string always be in alphabetical order or in the order of the original string? – Ben Mar 17 '21 at 13:53
  • [How to convert list to string [duplicate]](https://stackoverflow.com/questions/5618878/how-to-convert-list-to-string) [How to concatenate items in a list to a single string?](https://stackoverflow.com/questions/12453580/how-to-concatenate-items-in-a-list-to-a-single-string) – Zeinab Mardi Mar 17 '21 at 14:32

1 Answers1

3

Try this:

print("".join(sorted(set("aaabbbbccccdefklmopqwwxxxxxyyyyy"))))

Output:

abcdefklmopqwxy

As suggest in the comments by @Matthias you can get the job done with itertools, but this time the order is based on the original string.

import itertools
print(''.join(g[0] for g in itertools.groupby("aaabbbbccccdefklmopqwwxxxxxyyyyy")))

Output:

abcdefklmopqwxy
baduker
  • 19,152
  • 9
  • 33
  • 56
  • 4
    This assumes that the order of the output will always be alphabetical rather than the order of the original string. – Ben Mar 17 '21 at 13:53
  • 1
    `itertools` to the rescue: `b = ''.join(g[0] for g in itertools.groupby(a))` – Matthias Mar 17 '21 at 15:49