简体   繁体   English

如何计算输入中列表中的单独项目

[英]How to count separate items in list from input

Making a simple program which responds to the user's inputs. 制作一个响应用户输入的简单程序。

In the example, the input "I like math", would return the response "That's cool". 在示例中,输入“我喜欢数学”将返回响应“那很酷”。 I want the response to change if the user inputs two items from the list, for example "I like math and biology". 如果用户从列表中输入两个项目,例如“我喜欢数学和生物学”,我希望响应更改。

I decided to get this done by using the count function, but it always returns the value 0. What would I have to do to get the if response? 我决定通过使用count函数来完成此操作,但是它始终返回值0。我应该做些什么才能获得if响应?

list = ["math", "physics", "biology", "computer science"]
favsub = input("What are your favorite subjects? \n")
favsub = favsub.lower()
favsub = favsub.split()
num = favsub.count(list)
if num == 2:
    print("Both?")
else
    print("That's cool")

The code above is a simplified example, if you want to see the actual code, I'll leave it in a google doc here . 上面的代码是一个简化的示例,如果您想查看实际的代码,请在此处将其保留在google文档中。

list.count(x) only counts a single element x . list.count(x)仅计算单个元素x Of course, your list is not an element of favsub and it does not magically sum the counts of its elements. 当然,您的列表不是favsub的元素,并且不会神奇地求和其元素的数量。 However, building on your approach, you can do the following, using sum : 但是,根据您的方法,可以使用sum进行以下操作:

favsub = input("...").lower()
# do not split, otherwise you can't count "computer science" 
num = sum(x in favsub for x in list)

Generally, you should not name variables list (or str , int , etc.) as it shadows built-in names. 通常,您不应命名变量list (或strint等),因为它会隐藏内置名称。

You could make list and favsub sets instead, and the take the length of the intersection. 您可以改为设置list和favsub集,并采用相交的长度。

NOTE: This will only work if all the phrases in list and favsub are single words, without a space. 注意:仅当list和favsub中的所有短语都是单个单词且没有空格时,此方法才有效。

Your approach is too naive. 您的方法太幼稚。 First you will have to tokenize the user input (what if the user inputs math,physics instead of math and physics ? .split will not separate math from physics in this case). 首先,您必须标记用户输入(如果用户输入math,physics而不是math and physics怎么办? .split在这种情况下不会将mathphysics分开)。

Then you will need to call favsub.count with each memeber of list (which is a bad variable name by the way as it shadows the built-in list ). 然后,你将需要调用favsub.count与各memeber list (这是因为它阴影内置的方式不好的变量名list )。

I will suggest another naive (but easier) approach. 我将建议另一种幼稚(但更简单)的方法。 Forget about splitting and tokenizing the user input. 无需拆分和标记化用户输入。 Simply search each recognized subject and sum the results: 只需搜索每个公认的主题并汇总结果:

 subjects_list = ["math", "physics", "biology", "computer science"]
 favsub = input("What are your favorite subjects? \n")
 favsub = favsub.lower()
 count = 0
 for subject in subjects_list:
     if subject in favsub:
         count += 1
 print(count)

This is essentially the same as @schwobaseggl's answer but with an explicit counter. 这基本上与@schwobaseggl的答案相同,但是有一个明确的计数器。

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

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