简体   繁体   English

如何对字符串中的字符进行排序,但只保留撇号

[英]How to sort characters in string but keeping only apostrophe

I can't seem to figure out how to sort characters in string but keeping apostrophe. 我似乎无法弄清楚如何对字符串中的字符进行排序,但保留撇号。 This is what I've done so far: 这是我到目前为止所做的:

def scramble_word(word) -> str:
    index = word.find("'")
    return word[0] + ''.join(sorted(word[1:index]+word[index+1:-1], key=str.lower)) + word[-1]


print(scramble_word("Mo'uSE"))  # -> Mo'SuE
print(scramble_word("qu'iz"))  # -> qi'uz

What I get is MoSue and qiuz. 我得到的是MoSue和qiuz。 How to insert apostrophe back to string? 如何将撇号插回字符串?

You can add the "'" again to the string, not the perfect way but works for current scenario. 您可以再次向字符串添加"'" ,而不是完美的方式,但适用于当前场景。

def scramble_word(word) -> str:
    index = word.find("'")
    stra = word[0] + ''.join(sorted(word[1:index] + word[index+1:-1], key=str.lower)) + word[-1]
    return stra[:index] + "'" + stra[index:]
def scramble_word(word) -> str:
    index = word.find("'")
    str = ''.join(sorted(word[0:index] + word[index+1:]))
    return str[:index] + "'" + str[index:]

print(scramble_word("Mo'uSE"))  # -> EM'Sou
print(scramble_word("qu'iz"))  # -> iq'uz

Hope this might help: 希望这可能有所帮助:

>>> def scramble_word(word) -> str:
    index = word.find("'")
    sorted_word = ''.join(sorted(word, key=str.lower)).replace("'", "")
    return sorted_word[:index] + "'" + sorted_word[index:]

>>> scramble_word("Mo'uSE")
"EM'Sou"
>>> print(scramble_word("qu'iz"))
iq'uz
>>> 

Code

def scramble_word(word, keep="'"):
    """Return a scrambled word by sorting middle letters; `keep` character if found."""
    apos_idx = word.find(keep)
    word = word.replace(keep, "")
    shuffled = [word[0]] + sorted(word[1:-1], key=str.lower) + [word[-1]]
    if apos_idx != -1: 
        shuffled.insert(apos_idx, keep)
    return "".join(shuffled)

Demo 演示

words = ["Mo'uSE", "qu'iz", "normal"]
for word in words:
    print(scramble_word(word))

# Mo'SuE
# qi'uz
# namorl

A more general solution that preserves all punctuation: 一个更通用的解决方案,保留所有标点符号:

import string


def scramble_word(word):
    """Return a word by sorting middle letters; preserve punctuation."""
    puncts = [(word.find(c), c) for c in word if c in string.punctuation and word.find(c) > 0]
    replaced = [c for c in word if c not in {i[-1] for i in puncts}]
    shuffled = [replaced[0]] + sorted(replaced[1:-1], key=str.lower) + [replaced[-1]]
    [shuffled.insert(pos, p) for pos, p in puncts]
    return "".join(shuffled)

words = ["Mo'uSE", "qu'iz", "normal", "P-u.n*c(t!u;a:tio>n"]
for word in words:
    print(scramble_word(word))

# Mo'SuE
# qi'uz
# namorl
# P-a.c*i(n!o;t:tuu>n

This is done by collecting all (position, punctuation) pairs in each word, removing them, performing the scrambling algorithm and finally reinserting the punctuation. 这是通过收集每个单词中的所有(位置,标点符号)对,删除它们,执行加扰算法并最终重新插入标点符号来完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM