python函数作为列表元素_函数将空白列表作为参数而不是单元素列表传递

deflistAssign(lst,index,item):"""Assigns item item to list lst at index index, returns modified list."""lst[index]=itemreturnlstdeflistInsert(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,itemifindex=='end':index=len(lst)lst.insert(index,item)returnlstdeflistSurgery(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,extraArgsparent=lstforindexinindices[:-1]:parent=parent[index]parent=f(parent,indices[-1],*extraArgs)returnlistSurgery(lst,indices[:-1],listAssign,parent)defparseStringToList(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 upb=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 occurringforcins:ifc==' ':# If c is a space, ignore it but send signal to break off any strings currently being written tob=True# Some elifs for c being non alphanumericelse:# If c is alphanumeric, append it to the string it's working onprintc,"got passed as an alphanumeric; lvl is",lvlassertc.isalnum()ifbort:# If the string it's working on isn't alphanumeric or doesn't exist, append a new stringl=listSurgery(l,[-1]*lvl+['end'],listInsert,'')b,t=False,Falsel=listSurgery(l,[-1]*(lvl+1),lambdax,y,z:listAssign(x,y,x[y]+z),c)printlreturnlwhileop!='exit'andop!='quit':# Keep a REPL unless the user types "exit" or "quit", in which case exitop=raw_input("> ")ifop=='help':pass# Print help stuffelifopin{'quit','exit'}:passelse:printstr(parseStringToList(op))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值