枚举
numerate()
numerate():函数用于一个可遍历的数据对象(如列表,元组或字符串)组合为一个索引序列
l1=['a','abc','jk','opop']
for index,value in enumerate(l1):
print(index,value)
0 a
1 abc
2 jk
3 opop
for l in enumerate('happy'):
print(l)
(0, 'h')
(1, 'a')
(2, 'p')
(3, 'p')
(4, 'y')
冒泡排序
numbers = [10,6,7,8,9,1,2,4,3]
temp=0
for i in range(len(numbers)):
for j in range(i+1,len(numbers)):
if numbers[j]<numbers[i]:
temp=numbers[i]
numbers[i]=numbers[j]
numbers[j]=temp
else:
pass
print(numbers)
numbers = [10,6,7,8,9,1,2,4,3]
temp=0
for i in range(len(numbers)):
for j in range(i+1,len(numbers)):
if numbers[j]<numbers[i]:
numbers[i],numbers[j]=numbers[j],numbers[i]
else:
pass
print(numbers)
|