1

bisect モジュールを使用したいのですが、しようとすると次のエラーが発生しますimport bisect

NameError: global name 'bisect_left' is not defined

そして、私が試したときのこのエラーfrom bisect import bisect_left:

ImportError: cannot import name bisect_left

私はPythonドキュメントからこの関数を使用しようとしています:

def index(a, x):
    'Locate the leftmost value exactly equal to x'
    i = bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return i
    else:
        return False

私は何を間違っていますか?

4

1 に答える 1

5

スクリプトに名前を付けましたbisect.py。標準ライブラリの代わりにインポートされています:

nidhogg:stackoverflow-3.4 mj$ cat bisect.py 
import bisect

bisect.bisect_left
nidhogg:stackoverflow-3.4 mj$ bin/python bisect.py 
Traceback (most recent call last):
  File "bisect.py", line 1, in <module>
    import bisect
  File "/Users/mj/Development/venvs/stackoverflow-3.4/bisect.py", line 3, in <module>
    bisect.bisect_left
AttributeError: 'module' object has no attribute 'bisect_left'
nidhogg:stackoverflow-3.4 mj$ echo 'from bisect import bisect_left' > bisect.py
nidhogg:stackoverflow-3.4 mj$ bin/python bisect.py 
Traceback (most recent call last):
  File "bisect.py", line 1, in <module>
    from bisect import bisect_left
  File "/Users/mj/Development/venvs/stackoverflow-3.4/bisect.py", line 1, in <module>
    from bisect import bisect_left
ImportError: cannot import name 'bisect_left'

マスクしないようにスクリプトの名前を変更します。

于 2014-10-18T20:30:16.753 に答える