【发布时间】:2020-10-23 12:25:52
【问题描述】:
我正在处理一个名为 numbers 的数组,该数组将创建 4 列,分别称为 (x)、(y)、(z),第四列用于程序中。
我希望如果两行的 x 和 y 值重合,那么根据它们的 c,其中一个将从主数组中删除(“0”z 值删除“1 ”,“1”z 值删除“2”,“2”z 值删除“0”)。
原始数组如下所示:
[[12 15 2 0]
[65 23 0 0]
[24 66 2 0]
[65 23 1 0]
[24 66 0 0]]
问题是当我尝试运行以下程序时,最后没有得到所需的数组。预期的输出数组如下所示:
[[12 15 2 0]
[65 23 0 0]
[24 66 2 0]]
我已经从下面的程序中摘录了一段
import numpy as np
#Array
numbers = np.array([[12,15,2,0],[65,23,0,0],[24,66,2,0],[65,23,1,0],[24,66,0,0]])
#Original Array
print(numbers)
#Lists to store x, y and z values
xs = []
ys = []
zs = []
#Any removed row is added into this list
removed = []
#Code to delete a row
for line1 in numbers:
for line2 in numbers:
if line1[0] == line2[0]:
if line2[1] == line2[1]:
if line1[2] == 1 and line2[2] == 0:
removed.append(line1)
if line1[2] == 0 and line2[2] == 2:
removed.append(line1)
if line1[2] == 2 and line2[2] == 1:
removed.append(line1)
for i in removed:
numbers = np.delete(numbers,i,axis=0)
for line in numbers:
xs.append(line[0])
ys.append(line[1])
zs.append(line[2])
#Update the original Array
for i in removed:
print(removed)
print()
print("x\n", xs)
print("y\n", ys)
print("z\n", zs)
print()
#Updated Array
print(numbers)
【问题讨论】:
-
你会使用 Pandas 吗?
-
这能回答你的问题吗? deleting rows in numpy array
-
我曾考虑使用 pandas,但无法获得正确的代码构造。
-
好吧,是也不是,因为我想确保我删除行的代码是准确的并且没有任何差异。
-
如果三个 duplicate 行的
c值分别为 0,1 和 2 - 哪个胜出?
标签: python arrays numpy-ndarray