给定一个字符串,如何随机转置两个字母?

2024-09-28 23:29:52 发布

您现在位置:Python中文网/ 问答频道 /正文

给定一个字符串:

s = 'The quick brown fox jumps over the lazy dog'

如何随机选择一个令牌,交换该令牌中的两个字母,并返回带有修改令牌的字符串?例如(*)

The quick brown fxo jumps over the lazy dog

在上面的示例中,随机选择令牌fox,并交换两个字符。你知道吗

到目前为止,我试图:

def swap_letters(string):
    s = list(string)
    s[0], s[len(s)-1] = s[len(s)-1].upper(), s[0].lower()
    string = ''.join(s)
    return string    


def foo(a_string):
    a_string_lis = a_string.split()
    token = random.choice(a_string_lis)
    return swap_letters(token)

然而,我得到超过2个字母转置,我不知道如何保持在字符串内的令牌顺序。你知道如何以更具Python性的方式获得(*)吗?你知道吗


Tags: the字符串stringdef字母quicklazyover
3条回答

您可以使用str.replace方法:

def swap_letters(string):
    s = list(string)
    s[0], s[len(s)-1] = s[len(s)-1], s[0]
    string = ''.join(s)
    return string

def foo(a_string):
    a_string_lis = a_string.split()
    token = random.choice(a_string_lis)
    return a_string.replace(token, swap_letters(token))

你可以这样做:

import random
random.seed(42)

s = 'The quick brown fox jumps over the lazy dog'


def transpose(text, number=2):

    # select random token
    tokens = text.split()
    token_pos = random.choice(range(len(tokens)))

    # select random positions in token
    positions = random.sample(range(len(tokens[token_pos])), number)

    # swap the positions
    l = list(tokens[token_pos])
    for first, second in zip(positions[::2], positions[1::2]):
        l[first], l[second] = l[second], l[first]

    # replace original tokens with swapped
    tokens[token_pos] = ''.join(l)

    # return text with the swapped token
    return ' '.join(tokens)


result = transpose(s)
print(result)

输出

The iuqck brown fox jumps over the lazy dog

更新

对于长度为1的字符串,上面的代码失败,类似这样的代码应该可以修复它:

def transpose(text, number=2):

    # select random token
    tokens = text.split()
    positions = list(i for i, e in enumerate(tokens) if len(e) > 1)

    if positions:

        token_pos = random.choice(positions)

        # select random positions in token
        positions = random.sample(range(len(tokens[token_pos])), number)

        # swap the positions
        l = list(tokens[token_pos])
        for first, second in zip(positions[::2], positions[1::2]):
            l[first], l[second] = l[second], l[first]

        # replace original tokens with swapped
        tokens[token_pos] = ''.join(l)

    # return text with the swapped token
    return ' '.join(tokens)
    def swap_letters(string,pos,string1):
        s = list(string)
        s[len(s)-1], s[len(s)-2] = s[len(s)-2], s[len(s)-1].lower()
        string = ''.join(s)
        string1[pos[0]]=string
        s1= ' '.join(string1)
        return s1    
    def foo(a_string):
        a_string_lis = a_string.split()
        print(a_string_lis)
        token = random.choice(a_string_lis)
        pos=[i for i in range(len(a_string_lis)) if a_string_lis[i]==token]
        return swap_letters(token,pos,a_string_lis)
    s='The quick brown fox jumps over the lazy dog'
    print(foo(s))

相关问题 更多 >