使用yield语句,模拟内置函数reversed(),实现对两三个元素降序排列
def myreversed(lists):
if isinstance(lists, list):
pass
else:
lists = list(lists)
length = len(lists)
# 第一级遍历
for index in range(length):
# 第二级遍历
for j in range(1, length - index):
if lists[j - 1] < lists[j]:
# 交换两者数据,这里没用temp是因为python 特性元组。
lists[j - 1], lists[j] = lists[j], lists[j - 1]
for i in lists:
yield i
a = myreversed((1, 2, 3, 4, 15, 6, 7, 8, 9))
for i in a:
print(i, end=' ')