简体   繁体   English

从元组列表中查找与用户输入对应的值

[英]Finding the values corresponding with user input from a list of tuples

As a foreword, I'm quite new to python, and coding in general.作为前言,我对 python 和一般的编码很陌生。

I'm trying to get the following code to find the specific values in the foodgroups tuple that match with user input (ie: Dairy, Nuts, and Grain) and attach them to Output (ie: Dairy and Nuts).我正在尝试获取以下代码以查找与用户输入(即:Dairy、Nuts 和 Grain)匹配的foodgroups元组中的特定值,并将它们附加到Output (即:Dairy 和 Nuts)。 The line with Output was gotten from another website when I was first making this.当我第一次做这个的时候, Output的那一行是从另一个网站上得到的。 The code works when the user provides an input that only contains one item without any symbols or spaces (ie: Dairy) but anything extra causes Output to be blank when printed.当用户提供的输入只包含一个没有任何符号或空格的项目(即:乳制品)但任何额外的内容都会导致Output在打印时为空白时,该代码有效。

userinput = input("Enter foodgroups ate in the last 24hrs : ").title()
foodgroups = ("Dairy","Nuts","Seafood","Chocolate")

Output = list(filter(lambda x:userinput in x, foodgroups))

if foodgroups[0] or foodgroups[1] or foodgroups[2] or foodgroups[3] in userinput:
  print(Output,"is present in your list, " + userinput)
else:
  print("Negative.")

I've thought of swapping around foodgroups and userinput , but that results in a TypeError, and turning the tuple into a string has Output always return blank.我想过交换foodgroupsuserinput ,但这会导致 TypeError,并且将元组转换为字符串会使Output始终返回空白。

I've asked others how to fix this, but they've had no better luck.我问过其他人如何解决这个问题,但他们没有更好的运气。 Any help is appreciated!任何帮助表示赞赏!

If userinput is a comma separated string then split it and use a list:如果 userinput 是逗号分隔的字符串,则将其拆分并使用列表:

userinput = input("Enter foodgroups ate in the last 24hrs : ")
foodgroups = ("Dairy","Nuts","Seafood","Chocolate")
uin = userinput.split(",")
grp = []
for x in uin:
  if x in foodgroups:
    grp.append(x)

grp is the user defined foods in foodsgroup grp是食物组中用户定义的食物

The main thing is that you want to use split to separate individual words from the user input into a list of words.最重要的是,您希望使用split将用户输入中的单个单词分离成单词列表。 I also swapped x and seafoods in your lambda.我还在你的 lambda 中交换了xseafoods

If the user separates each word by one or more spaces, here's how to change your code to work:如果用户用一个或多个空格分隔每个单词,以下是更改代码以使其工作的方法:

userinput = input("Enter foodgroups ate in the last 24hrs : ").title()
foodgroups = ("Dairy","Nuts","Seafood","Chocolate")

userfoods = userinput.split()

Output = list(filter(lambda x: x in userfoods, foodgroups))

print(Output,"is present in your list, " + str(userinput))

As other's mention, you need to use split() to separate individual items in the input:正如其他人所提到的,您需要使用split()来分隔输入中的各个项目:

userfoods = userinput.split()

But even after that your if condition isn't correct:但即使在那之后,您的if条件也不正确:

if foodgroups[0] or foodgroups[1] or foodgroups[2] or foodgroups[3] in userinput:

The thing to realize here is that or and in are operators that only work with the immediately adjacent 2 values.这里要意识到的是, orin适用于紧邻的 2 个值的运算符。 We can add parentheses to see how this works:我们可以添加括号来看看这是如何工作的:

if (((foodgroups[0] or foodgroups[1]) or foodgroups[2]) or (foodgroups[3] in userinput)):

This means that foodgroups[0] or foodgroups[1] evaluates to just the value of foodgroups[0] , so foodgroups[1] is basically ignored.这意味着foodgroups[0] or foodgroups[1]仅评估为foodgroups[0]的值,因此foodgroups[1]基本上被忽略。 This isn't what you want.这不是你想要的。 Instead, you need to check in for each item:相反,你需要检查in每个项目:

if foodgroups[0] in userinput or foodgroups[1] in userinput or foodgroups[2] in userinput or foodgroups[3] in userinput:

But as you can see this gets very lengthy.但正如你所看到的,这会变得非常冗长。 So using a loop or list comprehension or generator expression can reduce the amount of code you need to write as others have already shown.因此,使用循环或列表推导式或生成器表达式可以减少您需要编写的代码量,正如其他人已经展示的那样。

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

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