简体   繁体   中英

Compare multiple unique strings in a list

Edit: I am using Python 2.7

I have a given 'matrix' as shown below which contains multiple lists of strings. I want to sort through matrix and only print out the row(s) which only contain a specific set of strings.

Can any one give me a hint on how to go about this?

What I have tried so far:

matrix = [("One", "Two", "Three"),
("Four", "Five", "Six"),
("Seven", "Eight", "One"),
("Four", "Five", "Six"),
("One", "Three", "Six")]

for index, data in enumerate(matrix):
    if "One" and "Three" and "Six" in data:
        print data

desired output:

("One", "Three", "Six")

actual output(as of now):

('Four', 'Five', 'Six')
('Four', 'Five', 'Six')
('One', 'Three', 'Six')

Your test is incorrect, you want to test each string separately with in :

if "One" in data and "Three" in data and "Six" in data:

and does not group operands for the in test; each component is evaluated separately:

("One") and ("Three") and ("Six" in data):

which leads to the result of "Six" in data being returned; the other two values are certainly always True as they are non-empty strings.

The better approach is to use a set :

if {"One", "Three", "Six"}.issubset(data):

I would use sets for this:

matrix = [("One", "Two", "Three"),
("Four", "Five", "Six"),
("Seven", "Eight", "One"),
("Four", "Five", "Six"),
("One", "Three", "Six")]

target = {"One", "Three", "Six"}

for row in matrix:
    if target <= set(row):
         print row

Here, target <= set(row) checks whether target is a subset of set(row) .

The reason your code doesn't work is that the following:

if "One" and "Three" and "Six" in data:

is equivalent to:

if bool("One") and bool("Three") and ("Six" in data):

Since bool("One") and bool("Three") are True , the entire expression simply checks whether "Six" is in data .

The reason behind this is that you're misusing the and

try

"One" and "Three"

in interactive console - it would output True , because "One" and "Three" are 'cast' tobooleans, and they're treated as true values. So, for this to work you should rewrite the condition to

if "One" in data and "Three" in data and "Six" in data

Why don't you test it as a set:

for data in matrix:
    if set(["Three","Six","One"]).issubset(set(data)):
        print data

results in:

('One', 'Three', 'Six').

Notice that as you test as a set there is problem with the ordering.

Actually, with your if statement

 if "One" and "Three" and "Six" in data: 

you gets all list that contains Six ,( notice your output )

("Seven", "Eight", "One") and ("One", "Two", "Three") not printed because Six is not in you tuples:

Additionally, Every string (not "" ) if is true in python for example:

>>> if("One"):
...     print "Yes"
... else:
...     print "No"
... 
Yes

So you if expression

 if "One" and "Three" and "Six" in data: 

is equivalent to

 if True and True and "Six" in data: 

that is equivalent to:

 if "Six" in data:   

Where as you need where "One", "Three", and "Six" all are present so do like:

if  ( ("One" in data) and 
      ("Three" in data) and 
      ("Six" in data)
     ):

As @Martijn Pieters answered. In addition get one more technique:

>>> target = {"One", "Three", "Six"}
>>> for data in matrix:
...     if (set(map(tuple, data)) <= set(map(tuple, target))):
...             print data
... 
('One', 'Three', 'Six')
>>> 

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