列表反转函数一:
#!/user/bin/python
# -*- coding: UTF-8 -*-
def reverse(li):
for i in range(0, len(li)/2):
temp = li[i]
li[i] = li[-i-1]
li[-i-1] = temp
l = [1, 2, 3, 4, 5]
reverse(l)
print(l)
列表反转函数二:
def reverse(ListInput):
RevList=[]
for i in range (len(ListInput)):
RevList.append(ListInput.pop())
return RevList
函数的方法名也可以作为另一个函数的参数。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def add(x,y):
return x+y
def add_twice(func,x,y):
return func(func(x,y),func(x,y))##计算过程(5+10)+(5+10)
a=5
b=10
print(add_twice(add,a,b))
其中 add 方法在 add_twice 方法中作为一个参数被调用。