如何在python中按字母顺序排序而不重复字母?

时间:2018-07-03 13:43:26

标签: python python-3.x sorting

我正在尝试按字母顺序对输入字符串进行排序,但彼此之间不能重复两个字母。 (abcabc,不是aabbcc) 例如,如果输入为“ aaccbb”,则输出为“ abcabc” 现在我写了这行代码。但这只是按字母顺序排序。如何防止两个字母重复?

s = str(input())
s2 = sorted(s)
s3 = "".join(s2)
print(s3)

4 个答案:

答案 0 :(得分:3)

from collections import Counter

def layered_sort(s):
    if not s: return s
    c = Counter(s)
    keys = sorted(c)
    max_repeats = max(c.values())
    return "".join([k for i in range(max_repeats) for k in keys if c[k] > i])

test_cases = (
    "aaccbb",
    "aacb",
    "aaab",
    "the rain in spain falls mainly on the plain"
)

for s in test_cases:
    print(repr(s), "sorts to", repr(layered_sort(s)))

结果:

'aaccbb' sorts to 'abcabc'
'aacb' sorts to 'abca'
'aaab' sorts to 'abaa'
'the rain in spain falls mainly on the plain' sorts to ' aefhilmnoprsty aehilnpst ailn ailn ain n  '

答案 1 :(得分:1)

itertoolscount一起使用defaultdict来创建元组,该元组具有关于字符观察的累积计数器。然后使用sorted对这些元组进行自然排序,并且当我们join时,我们可以提取所需的元素。

from collections import defaultdict
from itertools import count

def goofy_sort(s):

    d = defaultdict(count)
    l = [(next(d[x]), x) for x in s]
    return "".join([y for _, y in sorted(l)])

使用@Kevin的测试用例

test_cases = (
    "aaccbb",
    "aacb",
    "aaab",
    "the rain in spain falls mainly on the plain"
)

print(*map(goofy_sort, test_cases), sep="\n")

abcabc
abca
abaa
 aefhilmnoprsty aehilnpst ailn ailn ain n  

答案 2 :(得分:1)

我写了一个递归函数来做到这一点。我将第一组排序后的字符串分割开,并与其余字符串保持相同

def last_part_sort(in_str):
    in_str = sorted(in_str)
    new_str = []
    exclude_ix = []

    for i in range(1, len(in_str)):
        if in_str[i] == in_str[i-1]:
            new_str.append(in_str[i])
            exclude_ix.append(i)

    lead_string = ''.join([in_str[i] for i in range(len(sorted(in_str))) if i not in exclude_ix])

    trail_string = ''.join([s for s in new_str])

    if trail_string == '':
        return lead_string

    return lead_string + last_part_sort(trail_string)

以您为例

last_part_sort(input_str)
out: 'abcabc'

更多重复

input_str = 'aaaabbcc'
out: 'abcabcaa'
input_str = 'aaaabbbcc'
out: 'abcabcaba'

答案 3 :(得分:-2)

您可以使用正则表达式:

import re

r = re.compile(r"(.)\1{0,}")
r.findall("aaaaaaaabbbbbbccdeeee") # ['a', 'b', 'c', 'd', 'e']

正则表达式的工作方式如下:

  • .匹配任何字符
  • (.)将该角色放入捕获组
  • \1成为第一个捕获组
  • {0,}期望该组重复零次或更多次