python(日常技术点记录)

循环遍历数组

# 第一种,最常用的,通过for in遍历数组。
colours = ["red","green","blue"]  
for colour in colours:  
    print colour   
# red  
# green  
# blue  

# for in结合range,先获得数组长度,然后根据索取值
colours = ["red","green","blue"]  
for i in range(0, len(colours)):  
    print i, colour[i]    
# 0 red  
# 1 green  
# 2 blue  

数组倒叙排列

arr = [1,2,3,4]
print (arr[::-1])
# [4,3,2,1] 不修改原数组
	
arr.reverse()
print (arr)
# [4,3,2,1] 修改了原数组
	
reversed_arr = []
for i in reversed(arr):
    reversed_arr.append(i)
print (reversed_arr)
# [1,2,3,4] 改回到原数组

上一篇:jquery


下一篇:11.2