<p>你可以使用<a href="https://docs.python.org/2/library/bisect.html" rel="nofollow noreferrer">bisect</a></p>
<pre><code>from bisect import bisect
l = ['a', 'c', 'e']
print(bisect(l,"d"))
2
</code></pre>
<p>要将其添加到列表中:</p>
<pre><code>from bisect import insort
l = ['a',"b", 'c', 'e']
insort(l, "d")
print(l)
insort(l, "f")
print(l)
['a', 'b', 'c', 'd', 'e']
['a', 'b', 'c', 'd', 'e', 'f']
</code></pre>
<p>如果希望更快地插入,可以使用<a href="https://pypi.python.org/pypi/blist/?" rel="nofollow noreferrer">blist</a>,其中使用insort维护排序列表是:</p>
<pre><code>O(log**2 n) vs O(n)
</code></pre>
<p>从对分导入</p>
<pre><code>from blist import blist
b = blist(["a", "b", "c", "e"])
insort(b, "f")
insort(b, "d")
print(b)
blist(['a', 'b', 'c', 'd', 'e', 'f'])
</code></pre>
<p>还有一个<a href="http://stutzbachenterprises.com/blist/sortedlist.html#blist.sortedlist" rel="nofollow noreferrer">blist.sortedlist</a>列表,您可以在其中使用<a href="http://stutzbachenterprises.com/blist/sortedlist.html#blist.sortedlist.L.add" rel="nofollow noreferrer">.add</a>:</p>
<pre><code>from blist import sortedlist
l = ['b',"a", 'c', 'e']
b = sortedlist(l)
b.add("f")
print(b)
sortedlist(['a', 'b', 'c', 'e', 'f'])
</code></pre>
<p>还有一个<a href="http://www.grantjenks.com/docs/sortedcontainers/" rel="nofollow noreferrer">sortedcontainers</a>库具有<a href="http://www.grantjenks.com/docs/sortedcontainers/sortedlist.html" rel="nofollow noreferrer">sortedlist</a>实现。你知道吗</p>