获取一个文件并打乱中间的所有字母

发布于 2024-12-25 13:34:18 字数 128 浏览 1 评论 0原文

我需要拿一个文件并打乱每个单词的中间字母,但我无法打乱第一个和最后一个字母,而且我只能打乱超过 3 个字符的单词。我想如果我可以将每个单词放入它们自己的单独列表中,其中所有字母都分开,我就可以找到一种方法来打乱它们。任何帮助将不胜感激。谢谢。

I need to take a file and shuffle the middles letters of each word but I can't shuffle the first and last letters, and I only shuffle words longer then 3 characters. I think I can figure out a way to shuffle them if I can put each word into their own separate list where all the letters are separated. Any help would be appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

有木有妳兜一样 2025-01-01 13:34:18
text = "Take in a file and shuffle all the middle letters in between"

words = text.split()

def shuffle(word):
    # get your word as a list
    word = list(word)

    # perform the shuffle operation

    # return the list as a string
    word = ''.join(word)

    return word

for word in words:
    if len(word) > 3:
        print word[0] + ' ' + shuffle(word[1:-1]) + ' ' + word[-1]
    else:
        print word

故意不实现随机播放算法。

text = "Take in a file and shuffle all the middle letters in between"

words = text.split()

def shuffle(word):
    # get your word as a list
    word = list(word)

    # perform the shuffle operation

    # return the list as a string
    word = ''.join(word)

    return word

for word in words:
    if len(word) > 3:
        print word[0] + ' ' + shuffle(word[1:-1]) + ' ' + word[-1]
    else:
        print word

The shuffle algorithm is intentionally not implemented.

公布 2025-01-01 13:34:18

看看 random.shuffle。它将列表对象洗牌到适当的位置,这似乎就是您的目标。你可以做这样的事情来打乱字母

`

def scramble(word):

    output = list(word[1:-1])
    random.shuffle(output)
    output.append(word[-1])
    return word[0] + "".join(output)`

只要记住随机导入

Look at random.shuffle. It shuffles a list object in place which seems to be what youre aiming for. You can do something like this for shuffling the letters around

`

def scramble(word):

    output = list(word[1:-1])
    random.shuffle(output)
    output.append(word[-1])
    return word[0] + "".join(output)`

Just remember to import random

美人迟暮 2025-01-01 13:34:18
#with open("words.txt",'w') as f:
#    f.write("one two three four five\nsix seven eight nine")

def get_words(f):
    for line in f:
        for word in line.split():
            yield word

import random
def shuffle_word(word):
    if len(word)>3:
        word=list(word)
        middle=word[1:-1]
        random.shuffle(middle)
        word[1:-1]=middle
        word="".join(word)
    return word

with open("words.txt") as f:
    #print list(get_words(f))
    #['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
    #print map(shuffle_word,get_words(f))
    #['one', 'two', 'trhee', 'four', 'fvie', 'six', 'sveen', 'eihgt', 'nnie']
    import tempfile
    with tempfile.NamedTemporaryFile(delete=False) as tmp:
        tmp.write(" ".join(map(shuffle_word,get_words(f))))
        fname=tmp.name

import shutil
shutil.move(fname,"words.txt")
#with open("words.txt",'w') as f:
#    f.write("one two three four five\nsix seven eight nine")

def get_words(f):
    for line in f:
        for word in line.split():
            yield word

import random
def shuffle_word(word):
    if len(word)>3:
        word=list(word)
        middle=word[1:-1]
        random.shuffle(middle)
        word[1:-1]=middle
        word="".join(word)
    return word

with open("words.txt") as f:
    #print list(get_words(f))
    #['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
    #print map(shuffle_word,get_words(f))
    #['one', 'two', 'trhee', 'four', 'fvie', 'six', 'sveen', 'eihgt', 'nnie']
    import tempfile
    with tempfile.NamedTemporaryFile(delete=False) as tmp:
        tmp.write(" ".join(map(shuffle_word,get_words(f))))
        fname=tmp.name

import shutil
shutil.move(fname,"words.txt")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文