jle*_*lee 0 python methods list
刚刚回顾了我的 Python 课程,发现我忘记了如何做到这一点。
def outsideIn2(lst):
'''(list)->list
Returns a new list where the middle two elements have been
removed and placed at the beginning of the result. Assume all lists are an even
length
>>> outsideIn2(['C','a','r','t','o','n'])
['r','t','C','a','o','n'] # rt moves to front
>>> outsideIn2(['H','i'])
['H','i'] # Hi moves to front so output remains the same.
>>> outsideIn2(['B','a','r','b','a','r','a',' ','A','n','n','e'])
['r','a','B','a','r','b,','a',' ','A','n','n','e'] # ra moves to front.
'''
length = len(lst)
middle1 = lst.pop((len(lst) / 2) - 1)
middle2 = lst.pop((len(lst) / 2) + 1)
lst.insert([0], middle1)
lst.insert([1], middle2)
return lst
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
middle1 = lst.pop((len(lst) / 2) - 1)
类型错误:需要整数参数,但得到了浮点数
我究竟做错了什么?
当您升级到 Python 3 时,“ /”运算符从整数除法更改为实数除法。切换到 ' //' 运算符。
| 归档时间: |
|
| 查看次数: |
2256 次 |
| 最近记录: |