擅长:python、mysql、java
<p><strong>编辑</p>
<p>你在评论中说,你想看看你的数字是否在列表中的元组范围内。你可以用同样的方法得到这个结果:</p>
<pre><code>s1 = [('874', '893'), ('964', '985')]
index = '879'
if [x for (x, y) in s1 if x <= index <= y]:
print(True)
</code></pre>
<p>输出:</p>
<pre><code>True
</code></pre>
<p>我强烈建议您要么将元组转换为int,要么在比较之前转换。例如,检查以下内容:</p>
<pre><code>s1 = [('800', '1001')]
index = '1000'
if [x for (x, y) in s1 if x <= index <= y]:
print(True)
</code></pre>
<p>这个例子不会输出,因为<code>'800' <= '1000' <= '1001'</code>将计算为<code>False</code>。转换为ints将提供正确的输出。你知道吗</p>
<p>要将元组转换为整数:</p>
<pre><code>[(int(x), int(y)) for (x, y) in s1]
</code></pre>