为什么在检查一个空字符串是否在另一个字符串中时返回True?

2024-10-01 15:34:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有限的大脑无法理解为什么会发生这种情况:

>>> print '' in 'lolsome'
True

在PHP中,等效比较返回false:

^{pr2}$

Tags: infalsetrue情况phpprint大脑pr2
3条回答

基本上,从数学上讲:

The empty set is a subset of every set

同样的逻辑也适用于此。您可以考虑''一个空集。因此,它是每个字符串集合的子集,因为它们必须是相同的类型。在

>>> a = ""
>>> b = "Python"
>>> a in b
True
>>> set(a).issubset(b)
True
>>> a = set() #empty set
>>> b = set([1,2,3])
>>> a.issubset(b)
True
>>> 

但要小心!子集和成员是different things。在

enter image description here

From the documentation

For the Unicode and string types, x in y is true if and only if x is a substring of y. An equivalent test is y.find(x) != -1. Note, x and y need not be the same type; consequently, u'ab' in 'abc' will return True. Empty strings are always considered to be a substring of any other string, so "" in "abc" will return True.

从您的print调用来看,您使用的是2.x

要深入了解,请查看字节码:

>>> def answer():
...   '' in 'lolsome'

>>> dis.dis(answer)
  2           0 LOAD_CONST               1 ('')
              3 LOAD_CONST               2 ('lolsome')
              6 COMPARE_OP               6 (in)
              9 POP_TOP
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE

COMPARE_OP是我们进行布尔运算的地方,查看insource code显示了比较发生的位置:

^{pr2}$

cmp_outcome is in the same file处,很容易找到下一条线索:

res = PySequence_Contains(w, v);

abstract.c中:

{
    Py_ssize_t result;
    if (PyType_HasFeature(seq->ob_type, Py_TPFLAGS_HAVE_SEQUENCE_IN)) {
        PySequenceMethods *sqm = seq->ob_type->tp_as_sequence;
        if (sqm != NULL && sqm->sq_contains != NULL)
            return (*sqm->sq_contains)(seq, ob);
    }
    result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
    return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
}

我们在documentation中找到了下一个函数:

objobjproc PySequenceMethods.sq_contains

This function may be used by PySequence_Contains() and has the same signature. This slot may be left to NULL, in this case PySequence_Contains() simply traverses the sequence until it finds a match.

further down in the same documentation

int PySequence_Contains(PyObject *o, PyObject *value)

Determine if o contains value. If an item in o is equal to value, return 1, otherwise return 0. On error, return -1. This is equivalent to the Python expression value in o.

其中''不是{},序列{}可以被认为包含它。在

引用PHP's ^{} documentation

mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

Find the numeric position of the first occurrence of needle in the haystack string.

因此,您实际尝试的是类似于下面所示的Python构造

>>> print 'lolsome' in ''
False

因此,您实际上应该编写如下所示的代码,以便在PHP中进行相应的比较

^{pr2}$

即使这样,它也会发出警告并返回false。在

PHP Warning: strpos(): Empty needle in /home/thefourtheye/Desktop/Test.php on line 3

bool(false)

我挖得更深,发现the source code corresponding to the ^{} function

    if (!Z_STRLEN_P(needle)) {
        php_error_docref(NULL, E_WARNING, "Empty needle");
        RETURN_FALSE;
    }

他们认为被搜索的空字符串是一个有问题的情况。因此,他们发出警告并返回false。除此之外,我找不到任何文件讨论为什么它被视为一个问题。在

就Python而言,这种行为在Comparisons section中定义得很好

Empty strings are always considered to be a substring of any other string, so "" in "abc" will return True.

相关问题 更多 >

    热门问题