提取字母并填充单词的斑点

时间:2019-03-18 16:02:04

标签: python for-loop while-loop

我正在尝试制作一个脚本,其中有一个单词(“管理员”),并且提取了组成该字母的字母 这个词。

每个循环后我正在寻找的输出是这样的:

第一个循环:提取字母“ d”

_ d _ _ _ _ _ _ _ _ _ _

第二个循环:提取字母“ m”

_ d m _ _ _ _ _ _ _ _ _

第3循环:提取字母“ a”

a d m _ _ _ _ _ _ _ _

现在,问题是我想一次只填补一个位置。如您在字母“ a”中看到的,当一个字母在一个字母中出现多次 单词,脚本将填满该字母所在的所有位置。

解决方案可能基于以下事实:脚本应提取索引而不是单词,并填充相应的位置 通过字典访问每个索引。我曾尝试这样做,但未能成功。

您对我如何使其工作有任何想法吗?

代码:

import time
import random


word = 'administrator'
letters_extracted = []

def string_word(word, letters_extracted):
    string = ''
    for letter in word:
        print("\nletter:", letter)
        print("letters_extracted:", letters_extracted)            
        if letter in letters_extracted:
            string += letter + " "
        else:
            string += ' _ '
    return string

letters_in_word = []
for letter in word:
    letters_in_word.append(letter)
    print("letters_in_word", letters_in_word)

while True:
    print(string_word(word, letters_extracted))
    new_letter = random.choice(letters_in_word)
    print("\nnew letter = ", new_letter)
    #in order to avoid to extract the same letter twice.
    letters_in_word.remove(new_letter)
    letters_extracted.append(new_letter)
    time.sleep(1)
    print(string_word(word, letters_extracted))
    if len(letters_in_word)==0:
        break

以下是我使用字典的尝试。为了提取随机字母,我将“ administrator”一词进行了改组,并将其放入名为“ word_dictionary_shuffled”的词典中。问题是,当for循环开始时,它遵循重新组合字典(word_dictionary_shuffled)中字母的顺序。因此,字母不会以原始单词(“管理员”)的顺序出现。顺序是改组单词的顺序。而且,下划线不会出来。

import time
import random

word = 'administrator'
word_shuffled = ''.join(random.sample(word, len(word)))

word_dictionary = dict(enumerate(word))
word_dictionary_shuffled = dict(enumerate(word_shuffled))

print("word_dictionary", dict(enumerate(word)))
print("word_dictionary_shuffled", dict(enumerate(word_shuffled)))



def string_word(word_dictionary, word_dictionary_shuffled):
    string = ''
    for index,value in word_dictionary_shuffled.items():
        if index in word_dictionary_shuffled:
            string += value + " "
        else:
            string += ' _ '
        print("\nindex", index)
        print("string = ", string)
    return string


while True:
    print(string_word(word_dictionary, word_dictionary_shuffled))

    time.sleep(1)

    print(string_word(word_dictionary, word_dictionary_shuffled))    

2 个答案:

答案 0 :(得分:2)

您不需要字典就可以将索引映射到字母。字符串本身已经可以通过字母索引进行索引。您应该改编索引而不是字母,以便可以遍历改编的索引来逐个屏蔽相应的字母:

import time
import random

word = 'administrator'

indices_shuffled = list(range(len(word)))
random.shuffle(indices_shuffled)
letters = ['_'] * len(word)
for i in indices_shuffled:
    letters[i] = word[i]
    print(''.join(letters))
    time.sleep(1)

示例输出:

_d___________
_d_____t_____
_dm____t_____
_dm__i_t_____
_dm__ist_____
_dm__ist_a___
_dm__ist_a_o_
adm__ist_a_o_
admi_ist_a_o_
admi_istra_o_
admi_istra_or
administra_or
administrator

答案 1 :(得分:0)

另一种解决方案:

import random

word = 'administrator'
result = [[x, '_'] for x in word]
for i in result:
    res = random.choice([x for x in result if x[1] == '_'])
    index = result.index(res)
    result[index][1] = result[index][0]
    print(''.join([x[1] for x in result]))

输出:

____n________
____n___r____
____n_s_r____
____n_s_r__o_
____n_s_r__or
___in_s_r__or
a__in_s_r__or
a__in_str__or
ad_in_str__or
ad_in_stra_or
ad_inistra_or
ad_inistrator
administrator