简体   繁体   English

无法循环打印所有内容

[英]Having trouble with for loop printing everything

I'm still fairly new to python and I have a project that I am doing that is about paths and files. 我对python还是很陌生,我正在做一个关于路径和文件的项目。 Unfortunately, I cannot post my code for that project or else I can get into trouble. 不幸的是,我无法发布该项目的代码,否则会遇到麻烦。 So I have made a practice problem that is similar to what I'm hoping to execute with my code and I'm wondering if someone could please help me out because I've been stuck on this for a few days. 因此,我遇到了一个练习问题,类似于我希望用我的代码执行的问题,我想知道是否有人可以帮助我,因为我已经坚持了几天。

Okay so what I want to do is after verifying the first input and entering the second input is to print all sentences within the list L that contained the second input inside of it. 好的,我想做的是在验证第一个输入并输入第二个输入之后,将列表L中包含第二个输入的所有句子打印出来。 Ex. 防爆。 if I have a list called 如果我有一个清单叫做

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

if my second input is the word dogs. 如果我的第二个输入是dogs一词。 I want it to print out all the sentences within the list that contain the word dogs. 我希望它打印出列表中包含dogs一词的所有句子。 So what I want to print out is the following. 因此,我要打印的内容如下。

dogs are cute
dogs are loyal
dogs rule

That is what I want to be able to print in the shell. 那就是我希望能够在外壳中打印的内容。 So these are the practice problems that I've made and have tried in two different ways. 因此,这些是我已经提出并以两种不同方式尝试过的练习问题。

this is the list: 这是清单:

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']

First way: 第一种方式:

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')

but in the shell it just print outs the first sentence in the list and then continues to output error each time I click enter. 但是在外壳程序中,它只是打印出列表中的第一句话,然后每次单击Enter时继续输出错误。

second way: 第二种方式:

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')

The shell will only give me the final sentence which is 'no need to say hi'. 外壳只会给我最后一句话,即“无需打招呼”。 But what I want to print is 但是我要打印的是

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

If someone could please help me out I would really appreciate it and thank you very much in advance. 如果有人可以帮助我,我将非常感谢,并在此先感谢您。

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])

Even I am new to Python..Hope it helps 即使我是Python的新手。希望它能对您有所帮助

If you don't need to repeat this process. 如果您不需要重复此过程。 Code below should work: 下面的代码应该工作:

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

If you need to repeat it: 如果需要重复:

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....

I'm not really sure I understand your question. 我不确定我是否理解您的问题。 I'm new to Python as well and might just as well not be able to see the full picture. 我也是Python的新手,可能还看不到完整的图片。 This is what I came up with. 这就是我想出的。

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

If you want to make a continuous loop: 如果要进行连续循环:

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

Hope this helps 希望这可以帮助

Use the regex module instead (represented with re ). 请改用regex模块(用re表示)。 For example: 例如:

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 first will import the regex module, which is required in this program. 首先import re将导入此程序所需的正则表达式模块。 for i in range(0, len(dog_sentences)): will then start a for loop that will help conduct the search of every single item in the list. for i in range(0, len(dog_sentences)):然后将启动for循环,这将有助于进行列表中每个项目的搜索。 len() is used as the length of the list. len()用作列表的长度。 Since the last number is one less than what is written, it will be useful to search through the list using dog_sentences[i] . 由于最后一个数字比写入的数字小一个,因此使用dog_sentences[i]搜索列表将很有用。

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

The first argument is the string or pattern you want to find in your string, which is your next item. 第一个参数是您要在字符串中找到的字符串或模式,这是您的下一项。 If there is no match, m is equal to None . 如果没有匹配项,则m等于None

if m is not None:
    print dog_sentences[i]

If there is a match ( m is not equal to None ), then print the appropriate item that contains the string within it. 如果存在匹配项( m不等于None ),则打印其中包含字符串的适当项目。

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

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