Python习题集(八)

简介: Python习题集(八)

每天一习题,提升Python不是问题!!有更简洁的写法请评论告知我!

https://www.cnblogs.com/poloyy/category/1676599.html

 

题目


要求:判断数组元素是否对称。例如[1,2,0,2,1],[1,2,3,3,2,1]这样的都是对称数组
用Python代码判断,是对称数组打印True,不是打印False,如:
x = [1, "a",  0, "2", 0, "a", 1]


解题思路


  1. 循环取值,循环次数只需要列表长度的一半
  2. 每次取头尾对称下标的值比较

 

答案


a, b, c = [1, 2, 0, 2, 1], [1, 2, 3, 3, 2, 1], [1, 2, 3, 4, 5]
def duicheng(lists):
    lens = len(lists)
    flag = True
    for i in range(0, int(lens / 2)):
        if lists[i] != lists[lens - 1 - i]:
            flag = False
            break
    print(flag)
duicheng(a)
duicheng(b)
duicheng(c)
相关文章
|
Python
Python习题集(十七)
Python习题集(十七)
100 0
|
Python
Python习题集(十六)
Python习题集(十六)
70 0
|
Python
Python习题集(十五)
Python习题集(十五)
88 0
|
Python
Python习题集(十四)
Python习题集(十四)
150 0
|
Python
Python习题集(十三)
Python习题集(十三)
115 0
|
Serverless Python
Python习题集(十二)
Python习题集(十二)
87 0
|
Python
Python习题集(十一)
Python习题集(十一)
65 0
|
Python
Python习题集(十)
Python习题集(十)
59 0
|
Python
Python习题集(九)
Python习题集(九)
95 0
|
算法 Python
Python习题集(七)
Python习题集(七)
128 0

热门文章

最新文章