Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么当x(t)为空时,输出为['a','b','a']。python_Python_String_List - Fatal编程技术网

为什么当x(t)为空时,输出为['a','b','a']。python

为什么当x(t)为空时,输出为['a','b','a']。python,python,string,list,Python,String,List,输出是['a','b','a'],应该是[] 但如果我更改以下内容: def x(t): return '' in t def t(aList): bList = aList[:] for i in bList: if x(i) == False: aList.remove(i) print aList t(['a','b','c']) 输出将为[]。在t中检查空字符串是否是t的子字符串。但是空字符串是每个字符串的子字

输出是['a','b','a'],应该是[]

但如果我更改以下内容:

def x(t):
    return '' in t

def t(aList):
    bList = aList[:]
    for i in bList:
        if x(i) == False:
            aList.remove(i)
    print aList

t(['a','b','c'])

输出将为[]。

在t中检查空字符串是否是t的子字符串。但是空字符串是每个字符串的子字符串。因此,您的条件始终返回True,并且不会从列表中删除任何内容。

字符串中始终包含空字符串。这是意料之中的

def x(t):
    return 'd' in t
因为在“a”和“b”中!你的意思是输出是['a','b','c']?
>>> '' in 'hello'
True
>>> '' in ''
True
>>> 'a' in ''
False
>>> 'a' in 'abc'
True
>>> 'd' in 'abc'
False
def t(aList):
    print [i for i in aList if i]