importing random words from a file without duplicates Python

久未见 提交于 2019-12-13 07:27:39

问题


I'm attempting to create a program which selects 10 words from a text file which contains 10+ words. For the purpose of the program when importing these 10 words from the text file, I must not import the same words twice! Currently I'm utilising a list for this however the same words seem to appear. I have some knowledge of sets and know they cannot hold the same value twice. As of now I'm clueless on how to solve this any help would be much appreciated. THANKS!

please find relevant code below! -(p.s. FileSelection is basically open file dialog)

def GameStage03_E():
    global WordList
    if WrdCount >= 10:
        WordList = []
        for n in range(0,10):
            FileLines = open(FileSelection).read().splitlines()
            RandWrd = random.choice(FileLines)
            WordList.append(RandWrd)
        SelectButton.destroy()
        GameStage01Button.destroy()
        GameStage04_E()
    elif WrdCount <= 10:
        tkinter.messagebox.showinfo("ERROR", " Insufficient Amount Of Words Within Your Text File! ")

回答1:


Make WordList a set:

WordList = set()

Then update that set instead of appending:

WordList.update(set([RandWrd]))

Of course WordList would be a bad name for a set.

There are a few other problems though:

  • Don't use uppercase names for variables and functions (follow PEP8)
  • What happens if you draw the same word twice in your loop? There is no guarantee that WordList will contain 10 items after the loop completes, if words may appear multiple times.

The latter might be addressed by changing your loop to:

    while len(WordList) < 10:
        FileLines = open(FileSelection).read().splitlines()
        RandWrd = random.choice(FileLines)
        WordList.update(set([RandWrd]))

You would have to account for the case that there don't exist 10 distinct words after all, though.

Even then the loop would still be quite inefficient as you might draw the same word over and over and over again with random.choice(FileLines). But maybe you can base something useful off of that.




回答2:


not sure i understand you right, but ehehe, line 3: "if wrdcount" . . where dit you give wrdcount a value ? Maybe you intent something along the line below?:

wordset = {}
wrdcount = len(wordset)
while wrdcount < 10:
    # do some work to update the setcode here
    # when end-of-file break


来源:https://stackoverflow.com/questions/34581664/importing-random-words-from-a-file-without-duplicates-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!