繁体   English   中英

如何打印仅包含列表中字母的单词?

[英]How to print words that only cointain letters from a list?

您好,我最近一直在尝试在Python 3中创建一个程序,该程序将读取一个包含23005个单词的文本文件,然后用户将输入9个字符字符串,程序将使用该字符串来创建单词并将其与文本文件。

我要打印包含4-9个字母的单词,并且该单词也包含在列表中间 例如,如果用户输入字符串“ anitsksem”,则单词中必须存在第五个字母“​​ s”。

这是我自己获得的成就:

# Open selected file & read
filen = open("svenskaOrdUTF-8.txt", "r")

# Read all rows and store them in a list
wordList = filen.readlines()

# Close File
filen.close()

# letterList index
i = 0
# List of letters that user will input
letterList = []
# List of words that are our correct answers
solvedList = []

# User inputs 9 letters that will be stored in our letterList
string = input(str("Ange Nio Bokstäver: "))
userInput = False

# Checks if user input is correct
while userInput == False:
   # if the string is equal to 9 letters
   # insert letter into our letterList.
   # also set userInput to True
    if len(string) == 9:
        userInput = True
        for char in string:
            letterList.insert(i, char)
            i += 1

    # If string not equal to 9 ask user for a new input
    elif len(string) != 9:
        print("Du har inte angivit nio bokstäver")
        string = input(str("Ange Nio Bokstäver: "))

# For each word in wordList
# and for each char within that word
# check if said word contains a letter from our letterList
# if it does and meets the requirements to be a correct answer
# add said word to our solvedList

for word in wordList:
    for char in word:
        if char in letterList:
            if len(word) >= 4 and len(word) <= 9 and letterList[4] in word:
                print("Char:", word)
                solvedList.append(word)

我遇到的问题是,它不打印包含我的letterList中的字母的letterList ,而是打印出至少包含我的letterList 一个字母的letterList 这也意味着某些单词会多次打印输出,例如,如果这些单词包含letterList中的多个字母。

我已经尝试解决这些问题了一段时间了,但是我似乎无法弄清楚。 我还尝试过使用排列来创建列表中字母的所有可能组合,然后将它们与我的wordlist进行比较,但是我认为,鉴于必须创建的组合数量,解决方案很慢。

    # For each word in wordList
    # and for each char within that word
    # check if said word contains a letter from our letterList
    # if it does and meets the requirements to be a correct answer
    # add said word to our solvedList
    for word in wordList:
        for char in word:
            if char in letterList:
                if len(word) >= 4 and len(word) <= 9 and letterList[4] in word:
                    print("Char:", word)
                    solvedList.append(word)

另外,由于我对python还是很陌生的,因此,如果您有任何要分享的常规技巧,我将不胜感激。

您得到多个单词,主要是因为您要遍历给定单词中的每个字符,并且如果该字符在letterList中, letterList可以追加并打印它。

而是基于单词而不是字符进行迭代,同时还使用with上下文管理器自动关闭文件:

with open('american-english') as f:
    for w in f:
        w = w.strip()
        cond = all(i in letterList for i in w) and letterList[4] in w
        if 9 > len(w) >= 4 and cond:
            print(w)

这里cond用于精简if语句, all(..)用于检查单词中的每个字符是否在letterListw.strip()用于删除任何多余的空格。

另外,要在输入为9字母时填充letterList请不要使用insert 相反,只需将字符串提供给list即可以类似但明显更快的方式创建列表:

这个:

if len(string) == 9:
    userInput = True
    for char in string:
        letterList.insert(i, char)
        i += 1

可以写成:

if len(string) == 9:
    userInput = True
    letterList = list(string)

有了这些更改,就不需要初始的openreadlines ,也不是letterList的初始化。

您可以尝试以下逻辑:

for word in wordList:
    # if not a valid work skip - moving this check out side the inner for-each will improve performance
    if len(word) < 4 or len(word) > 9 or letterList[4] not in word:
        continue
    # find the number of matching words
    match_count = 0
    for char in word:
        if char in letterList:
            match_count += 1
    # check if total number of match is equal to the word count
    if match_count == len(word):
        print("Char:", word)
        solvedList.append(word)

您可以使用lambda函数来完成此任务。 我只是在这里放置一个POC,留给您将其转换为完整的解决方案。

filen = open("test.text", "r")
word_list = filen.read().split()
print("Enter your string")
search_letter = raw_input()[4]

solved_list  = [ word for word in word_list if  len(word) >= 4 and len(word) <= 9 and search_letter in word]
print solved_list

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM