简体   繁体   English

比较列表中的多个唯一字符串

[英]Compare multiple unique strings in a list

Edit: I am using Python 2.7 编辑:我使用的是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 : 您的测试不正确,您想要使用in分别测试每个字符串:

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

and does not group operands for the in test; and不为组操作数in测试; each component is evaluated separately: 每个组件分别进行评估:

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

which leads to the result of "Six" in data being returned; 这导致返回"Six" in data"Six" in data的结果; the other two values are certainly always True as they are non-empty strings. 其他两个值肯定都是True,因为它们是非空字符串。

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) . 这里, target <= set(row)检查target是否是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 . 由于bool("One")bool("Three")True ,因此整个表达式只检查"Six"是否在data

The reason behind this is that you're misusing the and 这背后的原因是你误用了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. 在交互式控制台中 - 它会输出True ,因为“One”和“Three”是“cast”tobooleans,并且它们被视为真正的值。 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语句

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

you gets all list that contains Six ,( notice your output ) 你得到包含Six所有列表,( 注意你的输出

("Seven", "Eight", "One") and ("One", "Two", "Three") not printed because Six is not in you tuples: ("Seven", "Eight", "One")("One", "Two", "Three")没有打印,因为Six不在你的元组中:

Additionally, Every string (not "" ) if is true in python for example: 另外,每个字符串(不是"" )如果在python中为true,例如:

>>> 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: 你需要在哪里“One”,“Three”和“Six”都存在,所以这样做:

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

As @Martijn Pieters answered. 正如@Martijn Pieters回答的那样。 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')
>>> 

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

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