简体   繁体   English

如何在python的二维数组中找到多少个值可以整除为某个值

[英]How to find how many values are divisible in to certain value in 2d array in python

The following code generate random number of 2d arrays and I want to print how many values in each pair are divisible to 3. For example assume we have an array [[2, 10], [1, 6], [4, 8]].以下代码生成随机数的二维数组,我想打印每对中有多少个值可以被 3 整除。例如,假设我们有一个数组 [[2, 10], [1, 6], [4, 8] ]。 So the first pair which is [2,10] has 3 ,6 and 9 which are totally 3 and second pair has 3 and 6 which are totally two and last pair[4,8] has just 1 divisible to 3 which is 6. Therefore, The final out put should print sum of total number of divisible values which is 3+2+1=6因此,第一对 [2,10] 有 3 ,6 和 9,它们完全是 3,第二对有 3 和 6,它们完全是两个,最后一对 [4,8] 只有 1 可以被 3 整除,也就是 6。因此,最终输出应打印可整除值总数的总和,即 3+2+1=6

a=random.randint(1, 10)

b = np.random.randint(1,10,(a,2))
b = [sorted(i) for i in b]   
c = np.array(b)            
   
counter = 0;   
  
for i in range(len(c)): 
    d=(c[i,0],c[i,1])
    if (i % 3 == 0):  
        counter = counter + 1
print(counter)

One way is to count how many integers in the interval are divisible by 3 by testing each one.一种方法是通过测试每个整数来计算区间中有多少个整数可以被 3 整除。

Another way, and this is much more efficient if your intervals are huge, is to use math.另一种方法,如果你的间隔很大,这会更有效,那就是使用数学。

Take the interval [2, 10] .取区间[2, 10]

2 / 3 = 0.66; ceil(2 / 3) = 1 2 / 3 = 0.66; ceil(2 / 3) = 1 . 2 / 3 = 0.66; ceil(2 / 3) = 1

10 / 3 = 3.33; floor(10 / 3) = 3 10 / 3 = 3.33; floor(10 / 3) = 3 . 10 / 3 = 3.33; floor(10 / 3) = 3

Now we need to count how many integers exist between 0.66 and 3.33, or count how many integers exist between 1 and 3. Hey, that sounds an awful lot like subtraction!现在我们需要计算 0.66 到 3.33 之间存在多少个整数,或者计算 1 到 3 之间存在多少个整数。嘿,这听起来很像减法! (and then adding one) (然后添加一个)

Let's write this as a function让我们把它写成一个函数

from math import floor, ceil
def numdiv(x, y, div):
    return floor(y / div) - ceil(x / div) + 1

So given a list of intervals, we can call it like so:所以给定一个间隔列表,我们可以这样称呼它:

count = 0
intervals = [[2, 10], [1, 6], [4, 8]]
for interval in intervals:
    count += numdiv(interval[0], interval[1], 3)
print(count)

Or using a list comprehension and sum:或者使用列表理解和求和:

count = sum([numdiv(interval[0], interval[1], 3) for interval in intervals])

You can use sum() builtin for the task:您可以使用sum()内置任务:

l = [[2, 10], [1, 6], [4, 8]]

print( sum(v % 3 == 0 for a, b in l for v in range(a, b+1)) )

Prints:印刷:

6

EDIT: To count number of perfect squares:编辑:计算完全平方数:

def is_square(n):
    return (n**.5).is_integer()

print( sum(is_square(v) for a, b in l for v in range(a, b+1)) )

Prints:印刷:

5

EDIT 2: To print info about each interval, just combine the two examples above.编辑 2:要打印有关每个间隔的信息,只需结合上面的两个示例。 For example:例如:

def is_square(n):
    return (n**.5).is_integer()

for a, b in l:
    print('Pair {},{}:'.format(a, b))
    print('Number of divisible 3: {}'.format(sum(v % 3 == 0 for v in range(a, b+1))))
    print('Number squares: {}'.format(sum(is_square(v) for v in range(a, b+1))))
    print()

Prints:印刷:

Pair 2,10:
Number of divisible 3: 3
Number squares: 2

Pair 1,6:
Number of divisible 3: 2
Number squares: 2

Pair 4,8:
Number of divisible 3: 1
Number squares: 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 python 中查找二维数组中的值? - How to find a value in a 2D array in python? 如何在Python中的二维数组中找到值的索引? - How to find the index of a value in 2d array in Python? 如何在2D数组中查找值的地址 - How to find the address of a value in an 2D array 如何在由 1D NumPy 数组中的值表示的索引处获取值为 1 的 2D NumPy 数组 (Python) - How to get a 2D NumPy array with value 1 at indices represented by values in 1D NumPy array (Python) 如何基于列表中的某些值在Python中创建2D列表 - How to create a 2D List in Python Based on Certain Values in a list python:如何确定2D列表的特定列中是否包含值? - python: How to determined if a value is contained in a certain column of 2D list? 如何在不使用 numpy 或在 Python 中展平的情况下在二维数组中找到最小值? - How to find a minimum value in a 2D array without using numpy or flattened in Python? 如何删除二维数组中低于 0 的值并使用 python lambda map 对剩余值进行平方 - How to remove values below 0 in 2D array and squaring remaining value using python lambda map 如何从二维数组中选择值并添加到另一个二维数组 python - How can i select values from 2d array and add to another 2d array python Python Data Frame如何在2D数组中查找局部最大值 - Python Data Frame how to find the local maximum in a 2D array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM