[英]how would I find if numbers in an array are equal to each other in python
If I had an array formed for example [1,2,3,4,5,6], how would I be able to check if 1 and 6 are equal and then 2 and 5 and then 3 and 4 etc without knowing how many numbers in my array that I have?如果我有一个形成例如 [1,2,3,4,5,6] 的数组,我将如何检查 1 和 6 是否相等,然后是 2 和 5,然后是 3 和 4 等,而不知道有多少我的数组中的数字是多少? Finding if the array is symmetric判断数组是否对称
The data set is being generated randomly from different molecules so the data isn't consistent and I am trying to work this out for each molecule.数据集是从不同的分子随机生成的,因此数据不一致,我正在尝试为每个分子解决这个问题。
def checkList(numbers):
for i in range(len(numbers)//2+1):
j = len(numbers) -i -1
print(i,j)
if i<j:
if numbers[i] != numbers[j]:
return False
return True
print(checkList([1,2,3,2,1]))
We set two pointers.我们设置了两个指针。 One is from the start and the other one is from the end.一个是从头开始的,另一个是从头到尾的。 both are moving to the middle while checking each pointer's elements are equal until the middle.两者都移动到中间,同时检查每个指针的元素是否相等,直到中间。
Most Pythonic Way:最Pythonic的方式:
def solve(arr,n):
return all(a[i]==a[n-i-1] for i in range(n))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.