获取字符串中重复字母的索引

时间:2016-01-10 21:29:55

标签: string python-3.x indexing zelle-graphics

from graphics import *
import random 

def hangman(word):
    returnStuff = {'again':0, '1st':1}

    alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
                'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

    win = GraphWin("Hangman", 800, 550)
    win.setBackground("yellow")

    titleText = Text(Point(400,50), 'HANGMAN')
    titleText.setSize(24)
    titleText.setStyle('bold')
    titleText.draw(win)

    #Building the hangman base
    base = Line(Point(120,350),Point(230,350))
    base.draw(win)
    stand = Line(Point(175,350),Point(175,150))
    stand.draw(win)
    stand2 = Line(Point(175,150),Point(250,150))
    stand2.draw(win)
    stand3 = Line(Point(250,150),Point(250,180))
    stand3.draw(win)

    #drawing the empty lines for the word
    x1 = 150
    x2 = 180
    l = 0
    print(word)
    while l< len(word):
        wordLine = Line(Point(x1, 420),Point(x2,420))
        wordLine.draw(win)
        l+=1
        x1+=40
        x2+=40

    guessCounter = 0
    textCheck = 0
    invalidText = Text(Point(600,100), 'You did not enter a valid letter.')
    invalidText.setTextColor('red')
    indexes = []
    while guessCounter < 6:
        #text entry box
        textEntry = Entry(Point(600,180),10)
        textEntry.draw(win)
        guessText = Text(Point(600,150), 'Guess a letter:')
        guessText.draw(win)
        #user has to click this box to confirm the letter
        enterBox = Rectangle(Point(580,200), Point(620,220))
        enterBox.setFill('white')
        enterBox.draw(win)
        clickText = Text(Point(600,210), 'Enter')
        clickText.draw(win)

        click = win.getMouse()
        x = click.getX()
        y = click.getY()

        if 580 < x < 620 and 200 < y < 220:
            guess = textEntry.getText().lower().strip()
            if guess not in alphabet:
                if textCheck == 0:
                    invalidText.draw(win)
                    textCheck = 1

            else:
                if textCheck == 1:
                    invalidText.undraw()
                    textCheck = 0
                for letter in word:
                    if letter == guess:
                        indexes.append(word.index(guess))
                        print(indexes)


    win.getMouse()

    win.close()

    return returnStuff 

#list with various words pertaining to nanotechnology
words = ['nanotechnology', 'science', 'nanometre' , 'strength', 'chemistry',
         'small', 'molecule', 'light' , 'weight', 'technology', 'materials',
         'property', 'physics', 'engineering', 'matter', 'waterloo', 'nanobot',
         'reaction', 'structure', 'cells']

#picks a random word from the list
word = random.choice(words)

#this variable ensures it opens the game the first time
initialCall = 1
#stores the returnValue for the first call
returnValue = hangman(word)

#sets the initialCall to 0 after first call 
if returnValue['1st']==1:
    initialCall=0

#Calls the game function again if user wishes
while initialCall == 1 or returnStuff['again'] == 1:
    returnValue = hangman(word)

我正在使用Python图形制作Hangman。我为巨大的代码块道歉,一切正常,我只是认为它必须有用。我关注的代码部分是:

            else:
                if textCheck == 1:
                    invalidText.undraw()
                    textCheck = 0
                for letter in word:
                    if letter == guess:
                        indexes.append(word.index(guess))
                        print(indexes)

当用户的字母猜测在字母表中时,将执行此代码块,然后我会浏览所选单词中的每个字母,如果在任何时候单词中的字母与猜测字母相同,我将该字母的索引存储在一个空列表中,这样我就可以用它来告诉计算机在空行上绘制字母的位置。

它工作正常,除了单词中有重复的字母。例如,工程有3 es。不幸的是,.index()只记录第一次出现该字母时的索引,并忽略其他字母。 这方面的工作是什么,所以我可以得到该单词中所有3个es的索引,而不是第一个e的3个索引。出于测试目的,我打印了所选单词和控制台上的索引列表,所以我可以看到发生了什么,所以我实际上不必猜一封信。

1 个答案:

答案 0 :(得分:0)

你可以做这样的事情

def indexes(word,letter):
    for i,x in enumerate(word):
        if x == letter:
            yield i

测试

>>> list( indexes("engineering","e") )
[0, 5, 6]
>>> 

这个函数是一个generator,这是一个懒函数,只有在被要求时才给出结果,为了获得你使用next的单个结果,函数执行到第一个{{3}然后返回结果并停止执行并记住它的位置,直到另一个对next的调用执行从上一点到下一个yield的女巫点恢复执行,如果不再提高StopIteration,例如:

>>> word="engineering"
>>> index_e = indexes(word,"e")
>>> next(index_e)
0
>>> print(word)
engineering
>>> next(index_e)
5
>>> next(index_e)
6
>>> next(index_e)
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    next(index_e)
StopIteration
>>> 

使用此函数的结果更新列表,可以使用extend方法

>>> my_list=list()
>>> index_e = indexes(word,"e")
>>> my_list.extend( index_e )
>>> my_list
[0, 5, 6]
>>> 

生成器用于其结果只是其他内容的中间步骤的情况,因为它们使用最少量的内存,但在这种情况下,您可能想要整个事情所以使用它作为第一个示例或重新创建函数像这样首先返回一个列表

def indexes(word,letter):
    result = list()
    for i,x in enumerate(word):
        if x == letter:
            result.append(i)
    return result

对不起,如果我把你的收益混淆了。