[Python] Finding all valid eight letter words by adding one extra letter from seven letter...

Discussão em 'Python' iniciado por Stack, Outubro 25, 2024.

  1. Stack

    Stack Membro Participativo

    I have several long list consisting of thousand of words sorted by length, one list for every word length. For every word of length n in a list I would like to generate all valid words from the list with words of length n+1, that is just by adding one extra letter.

    By trial and error I have found something that does the job but it is messy and runs for ages as I am very new to Python. I struggled a lot with all the variations with duplicate letters... What is a better and more efficient way to get the the job done?

    SevenPlusOneList = []
    for word7 in ListOfSevens:
    SevenPlusOne = []
    SevenCount = collections.Counter(word7)
    for word8 in ListOfEights:
    EightCount = collections.Counter(word8)
    NotSharedLetters = {k: SevenCount[k] for k in EightCount if
    k in EightCount and SevenCount[k] != EightCount[k]}
    if len(NotSharedLetters) == 1 and len(EightCount) >=
    len(SevenCount) and (len(EightCount)-len(SevenCount)==1
    or list(NotSharedLetters)[0] in SevenCount):
    SevenPlusOne.append(word8)
    if len(SevenPlusOne) > 0:
    SevenPlusOneList.append(SevenPlusOne)
    else:
    SevenPlusOneList.append('-')

    Continue reading...

Compartilhe esta Página