Python: Best Way to remove duplicate character from string

后端 未结 7 732
借酒劲吻你
借酒劲吻你 2020-12-01 21:46

How can I remove duplicate characters from a string using Python? For example, let\'s say I have a string:

foo = \"SSYYNNOOPPSSIISS\"

How c

相关标签:
7条回答
  • 2020-12-01 22:12

    Using itertools.groupby:

    >>> foo = "SSYYNNOOPPSSIISS"
    >>> import itertools
    >>> ''.join(ch for ch, _ in itertools.groupby(foo))
    'SYNOPSIS'
    
    0 讨论(0)
提交回复
热议问题