[Python] How do I check a randomized list against a base list to re-randomize in case it comes...

Discussão em 'Python' iniciado por Stack, Junho 22, 2025.

  1. Stack

    Stack Membro Participativo

    For context, I'm trying to make a script to help automate the creation of a worksheet I make weekly for my students, and in the worksheet I want to randomize words within a list taken from a text document. I have it working for the most part but it seems I have to run it 2-3 times to get it to actually randomize shorter words (around 3 digits) and I'm trying to figure out how I can just get it to check for that within the script do the randomizing again until it's properly randomized.

    import random
    from e2k import P2K #importing e2k phoneme to kana converter
    from g2p_en import G2p #gets g2p library

    #------------------------------------------------------------------------------
    #section for basic variables
    p2k = P2K() #initializing the phoneme to kana converter
    g2p = G2p() #initializing the g2p converter
    pronunciationList = [] #sets up list for pronunciations
    soundOutList = [] #sets up list for words
    #------------------------------------------------------------------------------


    with open("SoundOutInput.txt", "r") as file: #reads file and puts to list, removing whitespace. "r" is for read only
    for line in file:
    soundOutList.append(line.strip().split("\t")) #formats the words into the list (use * when printing or writing to new file to remove [""]

    randomizeList = soundOutList.copy() #sets up list for randomized words copying og list
    #------------------------------------------------------------------------------
    #test
    #testKana = p2k(g2p(*soundOutList[3]))
    #print(testKana) #prints the randomized list to console for testing
    #------------------------------------------------------------------------------

    #------------------------------------------------------------------------------
    def randomSpelling(): #self explanatory function to randomize the words in the list

    for i in range(len(randomizeList)): #loops through each word in the list and randomizes
    randomizeList = ''.join(random.sample(*randomizeList,len(*randomizeList)))

    return randomizeList #returns the randomized list

    def katakanaize(): #turn og list to kana

    for i in range(len(soundOutList)): #loops through each word in the list
    katakana = p2k(g2p(*soundOutList))
    #print(katakana) #prints the kana to console for testing
    pronunciationList.append(katakana)

    return pronunciationList #returns the kana list

    def printTests(): #tests to make sure lists work

    print("Sound Out Activity Words:", *soundOutList) #prints header
    print("Level 1 Words: ", *levelOneWords, *levelOneKana) #prints level 1 words
    print("Level 2 Words: ", *levelTwoWords, *levelTwoKana) #prints level 2 words
    print("Level 3 Words: ", *levelThreeWords, *levelThreeKana) #prints level 3 words
    print("Level 4 Words: ", *levelFourWords, *levelFourKana) #prints level 4 words
    print("Level 5 Words: ", *levelFiveWords, *levelFiveKana) #prints level 5 words


    #------------------------------------------------------------------------------

    #------------------------------------------------------------------------------
    katakanaize()
    randomSpelling()

    #------------------------------------------------------------------------------

    #grouping of the words into levels based on the difficulty
    #------------------------------------------------------------------------------
    levelOneWords = randomizeList[0:4] #first four randomized words, level 1 difficulty, followed by setting up lists for each level
    levelTwoWords = randomizeList[5:9]
    levelThreeWords = randomizeList[10:14]
    levelFourWords = randomizeList[15:19]
    levelFiveWords = randomizeList[20:22]

    levelOneKana = pronunciationList[0:4] #first four kana, level 1 difficulty, followed by setting up lists for each level
    levelTwoKana = pronunciationList[5:9]
    levelThreeKana = pronunciationList[10:14]
    levelFourKana = pronunciationList[15:19]
    levelFiveKana = pronunciationList[20:22]
    #------------------------------------------------------------------------------
    def randomSpellCheck():
    spellCheckBool = False
    while spellCheckBool == False: #loops through each word in the list
    if levelOneWords[0:4] == soundOutList[0:4]:
    randomizeList[0:4] = ''.join(random.sample(*randomizeList[0:4],len(*randomizeList[0:4])))
    elif levelTwoWords[5:9] == soundOutList[5:9]:
    randomizeList[5:9] = ''.join(random.sample(*randomizeList[5:9],len(*randomizeList[5:9])))
    elif levelThreeWords[10:14] == soundOutList[10:14]:
    randomizeList[10:14] = ''.join(random.sample(*randomizeList[10:14],len(*randomizeList[10:14])))
    elif levelFourWords[15:19] == soundOutList[15:19]:
    randomizeList[15:19] = ''.join(random.sample(*randomizeList[15:19],len(*randomizeList[15:19])))
    elif levelFiveWords[20:22] == soundOutList[20:22]:
    randomizeList[20:22] = ''.join(random.sample(*randomizeList[20:22],len(*randomizeList[20:22])))
    else:
    spellCheckBool = True
    #------------------------------------------------------------------------------
    randomSpellCheck()
    #printTests() #prints the lists to the console
    print(soundOutList[0:4]) #prints the first four words to console for testing
    print(pronunciationList[0:4]) #prints the first four kana to console for
    print(levelOneWords)


    I have printing set up to test the lists to see if there are any obvious things I'm missing and it seems the base list is formatted with extra brackets whereas the one randomized which is based on a copy of the original list doesn't have them which makes me think that's where my main problem is coming from. If anyone could tell me what I'm doing wrong here or what I have to do to fix this issue, I'd appreciate it greatly o7

    Continue reading...

Compartilhe esta Página