擅长:python、mysql、java
<p>没有测试过“所有iterables”,但尝试使用应该有用的习惯用法</p>
<p><strong>编辑了</strong>每个评论的新要求:(看起来只是一个更大的范围尝试/除了工作)</p>
<pre><code>a, b = (2, ), (2, 1)
def contains(a, b):
# a may be "any iterable", using generator to access
ag = (i for i in a)
try:
# initialize history with 1st len(b) elements of a
history = [next(ag) for _ in b]
while True: # exits with a match or a StopIteration exception
# check if all elements in iterables: history, b are equal
# independent of b container type
# deMorgan logic in list comprehension to "and" matches,
# list comprehension returns [] if every element matches
if not [1 for j, e in zip(history, b) if j != e]:
return True
# advance history contents
history.pop(0)
history.append(next(ag))
except StopIteration:
return False
</code></pre>
<p>是的,我读到pop(0)是低效的</p>