提取文本文件中的第一个单词,然后提取相应的单词?

时间:2017-02-12 19:06:06

标签: python loops for-loop text-files

我最近发现了这段代码,但一直在努力弄清楚它是如何工作的。 " 111.txt"是一个包含列表行的文本文件,其中每行的列表的第一部分是解决方案,该列表中的相应单词是与该解决方案相关的关键词。除了第8行(solutions[i[-1]] ...)之外,我理解其中的大部分内容。我一直在查找使用的不同模块,但我仍然不了解该行的作用及其工作原理。我是使用Python的新手,所以如果可能的话,我真的很感激这条线的简单解释。

提前致谢!

questionbank = []
with open ('111.txt') as questions:
    for line in questions:
        questionbank.append(line.strip().split(','))

solutions = {}
for i in questionbank:
    solutions[i[-1]] = i[0:len(i)-1]

def phone_problem():
    n = 2
    while n <3:
        problem = input("Phone problem?")
        for d,v in solutions.items():
            if any(word in problem for word in v):
                print(d)
                n = 4
            else:
                continue

phone_problem()

&#34; 112.txt&#34;的例子:

put your phone is rice, wet, damp, water, puddle
replace you screen, screen, crack, smash, shatter...

更新: 我已经添加了您的代码,但它仍然没有输出解决方案。它只是继续运行while循环,无论我输入什么作为问题。我真的不确定为什么,但是当你定义解决方案时可能会这样做。

import webbrowser,time
url = "https://www.google.co.uk/webhp?hl=en&sa=X&ved=0ahUKEwjNuLiL1vHRAhVjD8AKHdFEAiEQPAgD&gws_rd=cr&ei=zUiTWKKpF8P_UoSambgO#hl=en&q="
problem = input("What is the problem with you device?")
split = problem.split(" ")
keyList = []

def other():
        print("no solution")

questionbank = []


with open ('111.txt') as questions:
    for line in questions:
        questionbank.append(line.strip().split(','))
# the following line are probably the source of the problem(up to calling the phone_problem function)
solutions = {question[0]:question[1:] for question in questionbank}


def phone_problem():
    while True:
        for solution, key_words in solutions.items():
            if any(word in problem for word in key_words):
                print(solution)
                return 

phone_problem()
if keyList == []:
    with open("counter.txt", "r") as file:
        for lines in file:
                number = lines[0]
    file.close()
    text_file = open("help.txt", "w")
    text_file.write(str(int(number)+1))
    text_file.write("\n{}                      {}                              {}                                  {}                          {}".format(number,devType,brand,device,problem))
    text_file.close()
    other()
keyList = list(set(keyList))
for i in keyList:
    print("Solution:",i)

1 个答案:

答案 0 :(得分:1)

答案在下面的评论中......

solutions = {}
for i in questionbank:
    # i = ['put your phone is rice', 'wet', 'damp', 'water', 'puddle']
    # i[-1] means last thing in list = 'puddle'
    # i[0:len(i)-1] means everything in i except the last element
    #               which could be rewritten as i[:-1]
    solutions[i[-1]] = i[0:len(i)-1]
    # solutions['puddle'] = ['put your phone is rice', 'wet', 'damp', 'water']

我认为代码是错误的。难道解决方案的密钥不应该是该行的第一个元素吗?代码可以更好地编写如下。

solutions = {question[0]:question[1:] for question in questionbank}

def phone_problem():
    while True:
        problem = input("Phone problem?")
        for solution, key_words in solutions.items():
            if any(word in problem for word in key_words):
                print(solution)
                return