📌  相关文章
📜  找到某个数字的倍数的数字的位置

📅  最后修改于: 2022-05-13 01:54:30.829000             🧑  作者: Mango

找到某个数字的倍数的数字的位置

在这类问题中,给出一个数字,我们必须找到该数字的所有倍数的位置或索引。为了解决这个问题,我们使用了一个名为numpy.argwhere()的函数

句法:

numpy.argwhere(array)

示例 1:

有时我们需要找到可被 int 或 float 数整除的元素的索引。

Python3
# Importing Pandas and Numpy libraries
import pandas as pd
import numpy as np
  
# Creating a Series of random numbers
n_series = pd.Series(np.random.randint(1, 25, 15))
print("Original Series:\n")
print(n_series)
  
# Finding the indexes of numbers divisible by 3
res_index = np.argwhere(n_series % 3==0)
print("Positions of numbers that are multiples of 3:\n")
print(res_index)


Python3
# Importing Pandas and Numpy libraries
import pandas as pd
import numpy as np
  
# Creating a Series of random numbers
n_series = pd.Series(np.random.randint(1, 20, 10))
print("Original Series:\n")
print(n_series)
  
# Finding the indexes of numbers divisible by 3.5
res_index = np.argwhere(n_series % 3.5==0)
print("Positions of numbers that are multiples of 3.5:\n")
print(res_index)


输出:

在上面的例子中,我们找到了所有能被 3 整除的数的索引。

示例 2:

蟒蛇3

# Importing Pandas and Numpy libraries
import pandas as pd
import numpy as np
  
# Creating a Series of random numbers
n_series = pd.Series(np.random.randint(1, 20, 10))
print("Original Series:\n")
print(n_series)
  
# Finding the indexes of numbers divisible by 3.5
res_index = np.argwhere(n_series % 3.5==0)
print("Positions of numbers that are multiples of 3.5:\n")
print(res_index)

输出:

在上面的例子中,我们找到所有能被浮点数 3.5 整除的数的索引