# enumerate可以加索引
fruit = ["apple", 'orange', "banana", "watermelon","pomelo","pear","guava"]
#for x in enumerate(fruit): #(0, 'apple')
# print(x) #
for i, x in enumerate(fruit):
print(i, x)
print("-------------")
#反方向输出
for i, x in enumerate(reversed(fruit)):
print(i, x)
print("-------------")
#按照字典顺序输出
for i, x in enumerate(sorted(fruit)):
print(i, x)
# result:
0 apple
1 orange
2 banana
3 watermelon
4 pomelo
5 pear
6 guava
-------------
0 guava
1 pear
2 pomelo
3 watermelon
4 banana
5 orange
6 apple
-------------
0 apple
1 banana
2 guava
3 orange
4 pear
5 pomelo
6 watermelon
Python Lambda
anonymous_function = lambda x, y: x + y
print(anonymous_function(4, 6))
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)