简体   繁体   English

如果数组中的数字在 python 中彼此相等,我将如何找到

[英]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.

相关问题 如何在数组中找到N个连续数字相等? - How to find N consecutive numbers are equal in array? 我如何找到数组中每个单独列表的总和? - How would I find the sum of each individual list in my array? 如何找到两个总和等于Python中目标总和的索引? - How can I find the indices of any two of the numbers, whose sum is equal to the target sum in Python? 如何在列表中找到等于 python 中某个值的所有数字组合? - How can I find all combinations of numbers in a list that's equal to a certain value in python? 在Python列表中查找彼此相距一定距离的数字 - Find numbers in Python list which are within a certain distance of each other 如何用除零(Python,numpy)以外的其他数组的所有数字替换数组的所有数字? - How can I replace all numbers of an array with all numbers of an other array except of the zeros (Python, numpy)? 如何检查数组的所有值是否彼此相等? - How to check if all values of an array are equal to each other? (Python)如何找出是否有其他相等的相反符号的值? - (Python) How can I find out if there are other equal values of opposite sign? 有一个带有一些数字的数组。 除了一个,所有数字都相等。 我怎么才能在 python 中找到那个数字? - There is an array with some numbers. All numbers are equal except for one. How i can found only that number in python? 如何在 Django 中检查两个模型是否相等? - How can I check if two models equal each other in Django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM