简体   繁体   English

你如何让python在列表中找到特定的文本然后打印出来

[英]How do you get python to find specific text in a list and then print it

This is what I have so far:这是我到目前为止:

def lists():
    global ClientList, highList, moderateList
    ClientList = [    ["NeQua,High"],
                      ["ImKol,Moderate"],
                      ["YoTri,Moderate"],
                      ["RoDen,High"],
                      ["NaThe,Moderate"],
                      ["ReWes,Moderate"],
                      ["BrFre,High"],
                      ["KaDat,High"],
                      ["ViRil,High"],
                      ["TrGeo,High"]]
     highList = ["Running", "Swimming", "Aerobics", "Football", "Tennis"]
     moderateList = ["Walking", "Hicking", "Cleaning", "Skateboarding", "Basketball"]
     checkclient()

def checkclient():
    global ClientList, highList, moderateList
    answer = input("please enter the client ID: ")
    answer2 = next(answer for answer in ClientList)
    print(answer)

So I want to input the specific clientID, I want python to then find the client ID in the list, print the clientID with the intensity level (high or moderate) so I can use it later to ask the user how many minutes they spent exercising in the different activities based on whether their intensity was high or moderate.所以我想输入特定的clientID,我希望python然后在列表中找到client ID,用强度级别(高或中)打印clientID,以便我稍后可以使用它来询问用户他们锻炼了多少分钟在不同的活动中,根据他们的强度是高还是中等。

At the moment the code only prints the first part of the list regardless of what the variable answer is: ["NeQua, High"].目前,代码只打印列表的第一部分,而不管变量 answer 是什么:["NeQua, High"]。

Please can you tell me how to fix this and try to keep it simple as I am relatively new to Python.请你告诉我如何解决这个问题并尽量保持简单,因为我对 Python 比较陌生。

Thanks Cameron谢谢卡梅伦

Use a dictionary instead (and there's no need to wrap it in a function that does nothing but create global objects).改用字典(并且不需要将它包装在一个只创建全局对象的函数中)。

ClientList = {"NeQua":"High",
              "ImKol":"Moderate",
              "YoTri":"Moderate",
              "RoDen":"High",
              "NaThe":"Moderate",
              "ReWes":"Moderate",
              "BrFre":"High",
              "KaDat":"High",
              "ViRil":"High",
              "TrGeo":"High"}

You don't need to specify mutable objects like list s or dictionaries as global if all you want to do is mutate them.如果您只想改变它们,则不需要将可变对象(如list或字典)指定为global对象。 You only need global if you want local assignments to the same name to also assign to the global name.如果您希望对同名的本地分配也分配给全局名称,则只需要global More importantly, though, next() just returns the next element in an iterable.更重要的是, next()只是返回可迭代对象中的下一个元素。 As a list is an ordered sequence, a generator that you make out of it with answer for answer in ClientList will have the same order, and the next() of that (redundant, I might add) generator will always be the first element of ClientList , because you keep making a new generator.由于list是有序序列,因此您使用answer for answer in ClientList生成的生成器将具有相同的顺序,并且该(冗余,我可能会添加)生成器的next()将始终是第一个元素ClientList ,因为你一直在制作一个新的生成器。 If you want next() to proceed through the whole thing, you'd have to save it first.如果您希望next()完成整个过程,则必须先保存它。 However, none of that is necessary here.然而,这里没有必要。 Just access the dictionary.只需访问字典。 I use get() here to avoid errors if the user tries to access a user that doesn't exist.如果用户尝试访问不存在的用户,我在这里使用get()来避免错误。

def checkclient():
    answer = input("please enter the client ID: ")
    print(ClientList.get(answer, 'Not found.'))

checkclient()

Also note that a function must be defined before it is called (order matters).另请注意,函数必须在调用之前定义(顺序很重要)。

You might change it as follows:您可以按如下方式更改它:

def checkclient():
    global ClientList, highList, moderateList
    answer = input("please enter the client ID: ")  # input: NaThe
    try:
        answer2 = next(a for a in ClientList if answer in a) 
    except StopIteration:
        checkclient()
    else:
        print(answer2)  # NaThe,Moderate

next returns the first element of an iterable so you always get the first element of ClientList , so you need to filter out the ones containing your ID. next返回可迭代对象的第一个元素,因此您始终获得ClientList的第一个元素,因此您需要过滤掉包含您的 ID 的元素。

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

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