简体   繁体   中英

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

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

Test whether the choice is in any of the lists:

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.

choice in ls tests whether choice is an element of the list.

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

If one of those lists contains the choice , any will return True, False otherwise.

您可以使用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.

You should check against each list using the "in" keyword

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:

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.

I'd flatten the list containing [division, multiplication, subtraction, addition] and then test to see if choice is in that list.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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