简体   繁体   English

如何检查多个列表中是否存在变量?

[英]How can I check if a variable exists in multiple lists?

Here's an excerpt from my code! 这是我的代码摘录!

division = ["Division","Divide","/","div"]
multiplication =["*","x","times","multiply","multiplication","multiple"]
subtraction = ["-",'minus','subtract','subtraction']
addition = ['+','plus','addition','add']
root = ['root','squareroot','square root']
square = ['square','squared','power 2']

choice = input('calculation type')
print(choice == (division or multiplication or subtraction or addition))

So far, it only gives "False". 到目前为止,它仅给出“ False”。 How can I check if a variable exists in multiple lists? 如何检查多个列表中是否存在变量? I've tried to make lists inside of lists but I still get "False", Here's the code of that... 我试图在列表内创建列表,但仍然出现“ False”,这是该代码……

division = ["Division","Divide","/","div"]
multiplication = ["*","x","times","multiply","multiplication","multiple"]
subtraction = ["-",'minus','subtract','subtraction']
addition = ['+','plus','addition','add']
root = ['root','squareroot','square root']
square = ['square','squared','power 2']
basic_double = [division,multiplication,subtraction,addition]
basic_single = [root,square]
choice = input('calculation type')
print(choice == basic_double or basic_single)

Any help would be appreciated! 任何帮助,将不胜感激! :D thank you!!! :D谢谢!!!

Test whether the choice is in any of the lists: 测试choice是否在any列表中:

any(choice in ls for ls in [division, multiplication, subtraction, addition])

any returns True if at least one of the elements of the given iterable are truthy. 如果给定可迭代的至少一个元素为真,则any返回True。

choice in ls tests whether choice is an element of the list. choice in ls测试了choice是否为列表的元素。

choice in ls for ls in [division, multiplication, subtraction, addition] is a generator comprehension, meaning it's an iterator that returns the result of choice in ls for any possible ls in [division, multiplication, subtraction, addition] . choice in ls for ls in [division, multiplication, subtraction, addition]是一个生成器理解,这意味着它是一个迭代器,它为[division, multiplication, subtraction, addition]任何可能的ls返回choice in lschoice in ls结果。

If one of those lists contains the choice , any will return True, False otherwise. 如果这些列表中的一个包含了choiceany将返回真,否则为false。

您可以使用itertools.chain()来检查所有列表中的选项,如下所示:

if choice in chain(division, multiplication, subtraction, addition):

Right now, you're comparing a String, choice, to a series of lists. 现在,您正在将一个字符串选择与一系列列表进行比较。 This will never be True as you're comparing two different data types. 当您比较两种不同的数据类型时,这永远不会是True。

You should check against each list using the "in" keyword 您应该使用“ in”关键字检查每个列表

if choice in division:
     #this returns true if the String set to choice is in the list of objects in division.
else if choice in multiplication:
.
.
.

This comparison will help you find the word in the list that you're looking for. 这种比较将帮助您在要查找的列表中找到单词。

If you want to check if choise is in any of the four lists you provided in your example: 如果要检查选择是否在示例中提供的四个列表中的任何一个中:

if choice in division + multiplication + subtraction + addition:
   # do something when its in any of it.
   ...

Right now, your code is doing the following comparison inside the print function: 现在,您的代码正在print函数中进行以下比较:

choice == ["Division","Divide","/","div"] or ["*","x","times","multiply","multiplication","multiple"] or ["-",'minus','subtract','subtraction'] or ['+','plus','addition','add']

Because of short-circuiting , this reduces to: 由于短路 ,减少到:

choice == ["Division","Divide","/","div"]

Which always evaluates to False because choice is not a list. 因为choice不是列表,所以该值始终为False

I'd flatten the list containing [division, multiplication, subtraction, addition] and then test to see if choice is in that list. 我将对包含[division, multiplication, subtraction, addition]的列表进行[division, multiplication, subtraction, addition]然后测试以查看choice是否在该列表中。

flattened = [item for ls in [division, multiplication, subtraction, addition] for item in ls]

print(choice in flattened)

See this answer to this question for more details on flattening a list of lists in python. 有关在python中扁平化列表列表的更多详细信息,请参见此 问题的 答案

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

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