简体   繁体   English

用python中的列表验证返回的列表?

[英]Verify returned list with a list in python?

I have a function that returns me a single list. 我有一个函数,可以返回一个列表。 I'm trying to take what is returned and find if it exists in the list I already have. 我正在尝试获取返回的内容,并查找它是否已经存在于列表中。 Am I missing another 'for' loop to iterate through cat_colors list? 我是否错过了另一个通过cat_colors列表进行迭代的“ for”循环? Or is the 'color' returned in a way that I can't match it in the list? 还是以我在列表中无法匹配的方式返回“颜色”? Trying to understand it and if you have a better solution? 试图了解它,如果您有更好的解决方案?

for cat in cat_names:
    color = Get_Color(cat)
    print cat_colors
    print color

    if color not in cat_colors:
        print "fail"
    else:
        print "pass"

output from print: 打印输出:

['brown', 'grey', 'white', 'black', 'red', 'orange', 'green', 'blue']
['brown']
fail

['brown', 'grey', 'white', 'black', 'red', 'orange', 'green', 'blue']
['grey']
fail

...

['brown'] isn't in ['brown', 'grey', 'white', 'black', 'red', 'orange', 'green', 'blue'] . ['brown'] 不在 ['brown', 'grey', 'white', 'black', 'red', 'orange', 'green', 'blue'] The string 'brown' is. 字符串'brown'是。

You have two options here. 您在这里有两个选择。 If you know that color will always be a one-element list, you can do: 如果您知道color将始终是一个元素列表,则可以执行以下操作:

if color[0] in cat_colors:
   ...

If not, you could do: 如果没有,您可以这样做:

if any(c in cat_colors for c in color):
   ...
   # or all(c in cat_colors for c in color) based on your needs

You should check for color not in cat_colors. 您应该检查cat_colors中没有的颜色。 Instead you are checking with cat not in cat_colors. 相反,您正在使用不在cat_colors中的cat进行检查。

Change your code like this 像这样更改代码

for cat in cat_names:
    color = Get_Color(cat)
    print cat_colors
    print color

    if color[0] not in cat_colors:
        print "fail"
    else:
        print "pass"

Your Get_Color(cat) function must return a string 您的Get_Color(cat)函数必须返回一个字符串
but it returns a list that's why you are not getting correct output 但它返回一个列表 ,这就是为什么您未获得正确输出的原因

eg.
l1=['a','b','c','d']
l2=['a',['b'],['c','d']]

['b'] in l1
False
'b' in l1
True
['b'] in l2
True

['c'] in l2
False
['c','d'] in l2
True

And if you have cats with more than one color and you wanna know which colors of this perticular cat are in cat_colors: 而且,如果您的猫有不止一种颜色,并且您想知道这种猫科动物的哪些颜色在cat_colors中:

def Get_Color(cat):
    if cat == 'whatevername1':
        color = ['brown', 'grey']
    elif cat == 'whatevername2':
        color = ['white', 'black', 'pink']
    elif cat == 'whatevername3':
        color = ['purple']
    return color
cat_names = ['whatevername1', 'whatevername2', 'whatevername3']
cat_colors = ['brown', 'grey', 'white', 'black', 'red', 'orange', 'green', 'blue']
print 'Valid colors:',cat_colors
for index,cat in enumerate(cat_names):
    color = Get_Color(cat)
    print 'Cat %s has colors:' %index,color
    valid_colors = set(cat_colors).intersection(color)
    if valid_colors:
        print 'Valid colors for cat %s are:' %index, valid_colors
    else:
        print "There are no valid colors for %s. Are you sure this is a real cat?"%index

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

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