简体   繁体   English

检查列表中是否存在字符

[英]Check whether character exists in list

I'm trying to write a recursive function that checks whether a certain character exists in a list(the list can contain sublists). 我正在尝试编写一个递归函数,以检查列表中是否存在某个字符(列表可以包含子列表)。

So far I've come up with this but it doesn't seem to be working. 到目前为止,我已经提出了这个建议,但是它似乎没有用。 What am I doing wrong? 我究竟做错了什么?

    def exists(str, seq):
        if not seq:
            return 0
        elif isinstance(seq[0], list):
            return exists(seq[0]) + exists(seq[1:])
        elif str == seq[0]:
            return True
        else:
            return exists(seq[1:])

If I try 如果我尝试

    print(exists("c", [a,[b,c],d]))

I get name "a" is not defined 我得到的名称“ a”未定义

def exists(str, seq):

You should not use the built-in type str as the name of a variable. 您不应使用内置类型str作为变量的名称。

        return 0

For binary functions you should return False , not 0. 对于二进制函数,应返回False而不是0。

        return exists(seq[0]) + exists(seq[1:])

Calls to exist must have two arguments, not one. exist调用必须有两个参数,而不是一个。 And you should use or , not + . 并且您应该使用or ,而不是+

        return exists(seq[1:])

Calls to exist must have two arguments, not one. exist调用必须有两个参数,而不是一个。

print(exists("c", [a,[b,c],d]))

[a,[b,c],d] Means "a nested list containing the variables named a, b, c, and d". [a,[b,c],d]意思是“包含名为a,b,c和d的变量的嵌套列表”。 If you don't have variables with those names, this will crash with a NameError. 如果您没有使用这些名称的变量,则将导致NameError崩溃。 Perhaps you meant to use string literals. 也许您打算使用字符串文字。

def exists(s, seq):
    if not seq:
        return False
    elif isinstance(seq[0], list):
        return exists(s, seq[0]) or exists(s, seq[1:])
    elif s == seq[0]:
        return True
    else:
        return exists(s, seq[1:])

print(exists("c", ["a",["b","c"],"d"]))

"I get name "a" is not defined". “我得到的名称“ a”未定义”。

That is because it is not defined, assuming this is your full code. 这是因为未定义,假设这是您的完整代码。 The same applies to b , c and d . bcd

You need to either define those variables beforehand (1.) with some string objects, or write the letters as string literals themselves (2.): 您需要事先使用一些字符串对象定义这些变量(1.),或者将字母本身写为字符串文字(2.):

  1. a, b, c, d = "my_string1", "my_string2", "my_string3", "my_string4"
  2. print(exists('c', ['a', ['b', 'c'], 'd']))

Apart from that, you define your function as def exists(str, seq): , but you invoke it as exists(seq[0]) which means you are missing a parameter. 除此之外,您将函数定义为def exists(str, seq):但是将其调用为exists(seq[0]) ,这意味着您缺少参数。

Then you are trying to add boolean values, which might work, but I am not sure if it is what you want to achieve: return exists(seq[0]) + exists(seq[1:]) . 然后,您尝试添加可能有用的布尔值,但是我不确定这是否是您想要实现的值: return exists(seq[0]) + exists(seq[1:]) If converted to a boolean, 2 will also be True . 如果转换为布尔值,则2也将为True But it will also be True if one of the statements is 0 . 但是,如果其中一个语句为0也将为True To check whether both or none are True , you should multiply the values. 要检查两者是否均为True ,应将值相乘。 Also, try to use the same return type for every return case. 另外,请尝试对每种返回情况使用相同的返回类型。

You are also using the first character (or item) of your seq, while you actually wanted to use the first character of the string str . 您还使用了seq的第一个字符(或项目),而您实际上想使用字符串str的第一个字符。 Careful! 小心! str is a type class, do NOT use it as a variable name. str是类型类,请勿将其用作变量名。

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

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