文字列を別の関数で使用されるリストに解析する関数を書いています。実行する操作の1つは、特定の再帰の深さ(という名前の変数によって定義される深さ)で、(場合によっては深く再帰的な)リスト内の文字列に文字を付加することlvl
です。この操作はlistSurgery
、次のリストが前のリスト内のどこにあるかを示すインデックスのリストで呼び出されることになっている名前の関数であり、最後のインデックスは、操作を実行するディープリスト内のどのインデックスで呼び出されているかを示します。インデックスの空白のリストです。理由はわかりません。で呼び出されるはずのリストはですが[-1]
、デバッグでは、で呼び出されることが示されてい[]
ます。省略形のコードは次のとおりです。
def listAssign(lst,index,item):
"""Assigns item item to list lst at index index, returns modified list."""
lst[index] = item
return lst
def listInsert(lst,index,item):
"""Inserts item item to list lst at index index, returns modified list."""
print "listInsert just got called with these arguments:",lst,index,item
if index == 'end':
index = len(lst)
lst.insert(index,item)
return lst
def listSurgery(lst,indices,f,*extraArgs):
"""Performs operation f on list lst at depth at indices indices, returns modified list."""
print "listSurgery just got called with these arguments:",lst,indices,f,extraArgs
parent = lst
for index in indices[:-1]:
parent = parent[index]
parent = f(parent,indices[-1],*extraArgs)
return listSurgery(lst,indices[:-1],listAssign,parent)
def parseStringToList(s):
"""Takes in a user-input string, and converts it into a list to be passed into parseListToExpr."""
# ...
l = [] # List to build from string; built by serially appending stuff as it comes up
b = True # Bool for whether the parser is experiencing spaces (supposed to be True if last character processed was a space)
t = False # Bool for whether the parser is experiencing a string of non-alphanumeric characters (supposed to be True if last character was a non-alphanumeric character)
lvl = 0 # Keeps track of depth at which operations are supposed to be occurring
for c in s:
if c == ' ': # If c is a space, ignore it but send signal to break off any strings currently being written to
b = True
# Some elifs for c being non alphanumeric
else: # If c is alphanumeric, append it to the string it's working on
print c,"got passed as an alphanumeric; lvl is",lvl
assert c.isalnum()
if b or t: # If the string it's working on isn't alphanumeric or doesn't exist, append a new string
l = listSurgery(l,[-1]*lvl + ['end'],listInsert,'')
b, t = False, False
l = listSurgery(l,[-1]*(lvl+1),lambda x,y,z:listAssign(x,y,x[y]+z),c)
print l
return l
while op != 'exit' and op != 'quit': # Keep a REPL unless the user types "exit" or "quit", in which case exit
op = raw_input("> ")
if op == 'help':
pass # Print help stuff
elif op in {'quit','exit'}:
pass
else:
print str(parseStringToList(op))
私はでコードを呼び出してpython -tt code.py
入力しました1+1=2
、そしてこれは私が得たものです:
> 1+1=2
1 got passed as an alphanumeric; lvl is 0
listSurgery just got called with these arguments: [] ['end'] <function listInsert at 0x10e9d16e0> ('',)
listInsert just got called with these arguments: [] end
listSurgery just got called with these arguments: [''] [] <function listAssign at 0x10e9d10c8> ([''],)
Traceback (most recent call last):
File "analysis.py", line 276, in <module>
print str(parseStringToList(op))
File "analysis.py", line 218, in parseStringToList
l = listSurgery(l,[-1]*lvl + ['end'],listInsert,'')
File "analysis.py", line 63, in listSurgery
return listSurgery(lst,indices[:-1],listAssign,parent)
File "analysis.py", line 62, in listSurgery
parent = f(parent,indices[-1],*extraArgs)
IndexError: list index out of range
誰かがこれを説明できますか?なぜ代わりにlistSurgery
取得するのですか?は、であり、その時点で渡されるはずの引数はです。の代わりに呼び出される理由を気にしないでください。[]
[-1]
lvl
0
[-1]*(lvl+1)
['']
'1'