擅长:python、mysql、java
<p>Lua没有Python的<code>in</code>操作符的等价物,但是如果您知道要模拟的容器类型,那么为它编写函数就很容易了</p>
<h2><code>str</code></h2>
<p><code>true</code>参数确保子字符串按字面意义处理,而不是按模式处理。详情见<a href="https://www.lua.org/manual/5.4/manual.html#pdf-string.find" rel="nofollow noreferrer">^{<cd4>}</a></p>
<pre><code>local function inString(s, substring)
return s:find(substring, 1, true)
end
</code></pre>
<h2><code>list</code></h2>
<pre><code>local function inArray(array, x)
for _, v in ipairs(array) do
if v == x then
return true
end
end
return false
end
</code></pre>
<h2><code>dict</code></h2>
<pre><code>local function inKeys(t, k)
return t[k] ~= nil
end
</code></pre>