每天一习题,提升Python不是问题!!有更简洁的写法请评论告知我!
https://www.cnblogs.com/poloyy/category/1676599.html
如果有一个列表a=[1,3,5,7,11]
问题:1如何让它反转成[11,7,5,3,1]
2.取到奇数位值的数字,如[1,5,11]
直接调用内置函数或切片
切片,设置步长
"""
如果有一个列表a=[1,3,5,7,11]
问题:1如何让它反转成[11,7,5,3,1]
"""
# 非算法方案一
a = [1, 3, 5, 7, 11]
a = a[::-1]
print(a)
# 非算法方案二
a = [1, 3, 5, 7, 11]
a.reverse()
print(a)
# 算法
a = [1, 3, 5, 7, 11]
lens = len(a)
for i in range(0, int(lens / 2)):
a[i], a[lens - i - 1] = a[lens - i - 1], a[i]
print(a)
"""
如果有一个列表a=[1,3,5,7,11]
问题:2.取到奇数位值的数字,如[1,5,11]
"""
a = [1, 3, 5, 7, 11]
a = a[::2]
print(a)