【问题标题】:Having trouble with for loop printing everything循环打印所有内容时遇到问题
【发布时间】:2016-04-23 00:49:52
【问题描述】:

我对 python 还是很陌生,我正在做一个关于路径和文件的项目。不幸的是,我不能为那个项目发布我的代码,否则我会遇到麻烦。所以我做了一个类似于我希望用我的代码执行的练习问题,我想知道是否有人可以帮助我,因为我已经坚持了几天。

好的,所以我想做的是在验证第一个输入并输入第二个输入之后打印列表 L 中包含第二个输入的所有句子。前任。如果我有一个名为

的列表
dog_sentences = ['dogs are cute', 'dogs are loyal', 'hi cat', 'dogs rule']

如果我的第二个输入是狗这个词。我希望它打印出列表中包含单词 dogs 的所有句子。所以我要打印的内容如下。

dogs are cute
dogs are loyal
dogs rule

这就是我希望能够在 shell 中打印的内容。所以这些是我做的练习题,并以两种不同的方式尝试过。

这是列表:

L=['hi there', 'it was nice to say hi', 'saying hi this many times is awkward',
   'hey I can also say hola', 'no need to say hi']

第一种方式:

while True:
    match = input().strip()
    for sentence in L:
        if match == 'Next' + ' ' + 'step':
            print('match')
            new_word_match = input().strip()
            if new_word_match in sentence:
                print(sentence)
                break
        else:
            print('error')

但在 shell 中,它只是打印出列表中的第一句话,然后每次单击 enter 时都会继续输出错误。

第二种方式:

while True:
    match = input().strip()
    for sentence in L:
        print(sentence)
    if match == 'Next' + ' ' + 'step':
        print('match')
        new_word_match = input().strip()
        if new_word_match in sentence:
            print(sentence)
            break
    else:
        print('error')

shell 只会给我最后一句话,即“无需打招呼”。但我要打印的是

hi there
it was nice to say hi
saying hi this many times is awkward
no need to say hi

如果有人可以帮助我,我将不胜感激,并在此先感谢您。

【问题讨论】:

  • 那么你需要不断地接受这些输入吗?在这两个例子中你都有while True。第二个有break,但第一个没有while循环。
  • 因为我最初的问题是关于路径。我需要 while 循环,因为如果路径不存在,那么我需要打印单词 error,然后重复请求路径的过程,直到它有效。这就是我使用while循环的原因。我很抱歉没有早点写。

标签: python list if-statement input while-loop


【解决方案1】:

改用正则表达式模块(用re 表示)。例如:

import re

dog_sentences = ['dogs are cute', 'dogs are loyal', 'hi cat', 'dogs rule']
for i in range(0, len(dog_sentences)):
    m = re.match("dogs", dog_sentences[i])
    if m is not None:
        print dog_sentences[i]

import re 首先将导入正则表达式模块,这是该程序所必需的。然后for i in range(0, len(dog_sentences)): 将启动for 循环,该循环将帮助搜索列表中的每一项。 len() 用作列表的长度。由于最后一个数字比写入的数字小一,因此使用dog_sentences[i] 搜索列表会很有用。

m = re.match("dogs", dog_sentences[i])

第一个参数是你想在你的字符串中找到的字符串或模式,这是你的下一个项目。如果没有匹配,m 等于 None

if m is not None:
    print dog_sentences[i]

如果有匹配项(m 不等于 None),则打印其中包含字符串的相应项目。

【讨论】:

    【解决方案2】:

    我不确定我是否理解您的问题。我也是 Python 新手,可能无法看到完整的画面。这就是我想出的。

    searched_words = input("word you want to see in text: ")
    for i in L:
        if searched_words in i:
            print(i)
    

    如果要连续循环:

    ans = True
    while ans == True:
        searched_words = raw_input("word you want to see in text: ")
        for i in L:
            if searched_words in i:
                print(i)
        check = raw_input("Do you wish to continue searching for words in the list(y/n)?")
        if check == 'n': 
            ans = False
    

    希望对你有帮助

    【讨论】:

    • 我很抱歉我试图做的是为第二个输入编写的任何内容,以检查它是否在某个列表中。如果在一个字符串或更多字符串中找到它,则只打印那些包含该单词的字符串。就像狗的例子。如果我第二次输入狗这个词,那么应该只打印三个字符串。我的第二个代码是最接近的,因为它打印列表中的最后一件事,但它不会打印每个带有单词 dog 的字符串。对不起,我希望能解决它。
    【解决方案3】:

    如果您不需要重复此过程。下面的代码应该可以工作:

    input1 = input().strip()
    match_string = "Next Step"
    if input1 == match_string:
        input2 = input().strip()
        for sentence in sentences:
             if input2 in sentence:
                 print(sentence)
    

    如果需要重复:

    while True:
       input1 = input().strip()
       match_string = "Next Step"
       if input1 == match_string:
          input2 = input().strip()
          for sentence in sentences:
               if input2 in sentence:
                   print(sentence)
    
       ....some condition to break out....
    

    【讨论】:

      【解决方案4】:
      l = ["god is there", "human are there", "nobody there"]
      string = str(raw_input("which word r u lookin for: "))
      for i in range(0, len(l)):
          if(string in str(l[i])):
              print(l[i])
      

      即使我是 Python 新手..希望它有所帮助

      【讨论】:

        猜你喜欢
        • 2016-08-08
        • 1970-01-01
        • 2022-01-21
        • 1970-01-01
        • 2021-03-07
        • 2021-06-15
        • 2015-05-23
        • 1970-01-01
        • 2022-01-20
        相关资源
        最近更新 更多