range (l and (QSL-1))
应该是 range(QSL)
。
l and (QSL-1)
实际上将评估为0
,因为and
条件在第一个 Falsy 值处短路并返回该值。如果所有项目都是True
值,则返回最后一个项目。
>>> 0 and 2
0
>>> 1 and 2
2
>>> 1 and 0 and 3
0
>>> 1 and 2 and 3
3
在 python 中,您可以遍历列表本身,不需要索引:
for strs in QuerySplit:
if strs in i.text:
print string
range
:
>>> range(4)
[0, 1, 2, 3]
>>> range(2)
[0, 1]
帮助range
:
range(stop) -> list of integers
range(start, stop[, step]) -> list of integers
Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
These are exactly the valid indices for a list of 4 elements.