这是在C++中使用向量表示邻接列表的代码(来自极客)。
// A utility function to add an edge in an
// undirected graph.
void addEdge(vector<int> adj[], int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
// A utility function to print the adjacency list
// representation of graph
void printGraph
我使用它来迭代numpy矩阵的列:
import numpy as np
A = np.random.randn(4,3)
for c in A.T:
print c.shape # (4L,) => not a real column
c = np.array(c, ndmin=2).T
print c.shape # (4L, 1L) => OK
是否有更好的方法来迭代列,避免循环中的重新定义和双transpose**:** ( double )
for c in A.T:
c = np.array(c, ndmin=2).T
注:我已经
我感兴趣的是根据使用numpy的匹配布尔掩码为特定的轴从全局2D数组生成np.arrays列表(或np.arrays)。我想知道是否可以使用np.ma.mask()或类似的.
举个例子也许更好:
number= 10
x = np.linspace(0,number,num=number+1,dtype=int)
B = np.vstack((x%3==0, x%2==0, x%1==0))
X = np.vstack((x//3, x//2, x-1))
list_ = []
for i in range(1,number+1):
pointer = X[:,i][B[:,i]]
我记得有一次我在编写一个类,在定义__init__方法时,我试图设置一个属于同一个类的实例,如下例所示: class Complex:
def __init__(self, real, imag):
self.real = real
self.imag = imag
self.conjugate = Complex(real, - imag) # instance of the same class
Compex(1, 2) # RecursionError: maximum recursion depth exceeded 但是当我尝试该
我正在和一只熊猫DataFrame一起工作,它代表了一个图表。数据文件由指示节点端点的MultiIndex索引。
设置:
import pandas as pd
import numpy as np
import itertools as it
edges = list(it.combinations([1, 2, 3, 4], 2))
# Define a dataframe to represent a graph
index = pd.MultiIndex.from_tuples(edges, names=['u', 'v'])
df = pd.Data
我有一个numpy像素数组( 0或255),我使用.where提取其所在位置的元组。现在,我想使用这些元组将1添加到单独的2D numpy数组中。是使用如下所示的for循环的最佳方式,还是有更好的类似于numpy的方式?
changedtuples = np.where(imageasarray > 0)
#If there's too much movement change nothing.
if frame_size[0]*frame_size[1]/2 < changedtuples[0].size:
print "No change- too m
我有一个名为added的python列表,它包含156个单独的列表,其中包含两个cols引用和一个数组。一个例子如下:
[0, 1, array]
问题是,我有重复的,虽然它们并不准确,因为列引用将被翻转。以下两项将完全相同:
[[0, 1, array], [1, 0, array]]
我尝试删除重复项的方法是对数字进行排序,检查是否有相同的数字,如果相同,则将结果附加到新的列表中。
这两种情况都导致了单独的错误:
for a in range(len(added)):
added[a][0:2] = added[a][0:2].sort()
TypeError: can only
我有一个numpy数组,filtered__rows,由LAS data [x, y, z, intensity, classification]组成。我创建了一个点的cKDTree,并找到了最近的邻居,query_ball_point,它是点及其邻居的索引列表。
是否有一种方法可以筛选filtered__rows以创建一个仅包含索引在query_ball_point返回的列表中的点的数组?
我做了这个射线追踪器来模拟两个镜头。每当光线击中屏幕时,屏幕上的位置就会被选中。然而,它运行得太慢了。它与射线追踪器无关,只是射线的绝对数量(128^4)。我知道多线程对此不起作用,但是如何使用multiprocessing在屏幕上对像素进行投票。这让我心烦意乱--我怎么让每个孩子投票给一个像素。每条射线都是独立的。下面是一些示例代码。创建输入数据也有一个问题。只需花很长时间才能将射线阵列发送到多处理类。
基本上,我希望内部循环产生多达8个子进程--让这些子进程投票--然后释放一个插槽……我认为即使这样也会运行得很慢。
import numpy as np
from mpl_toolkits.
我正在尝试创建一个转换数组的代码
我来自Matlab,我创建了这段代码(wich工作得很好):
A = [1,2,3,0,-2,0 ; 0,2,0,1,2,3];
n = size(A);
for i=1:n(2)
for j=1:n(1)
M(i,j) = A(j,i)
end
end
在Python中,我使用tryng (使用相同的逻辑):
M = [
[1,2,3,0,-2,0],
[0,2,0,1,2,3]
]
LM = (len(M),len(M[0]))
print(LM)
Maux=[[]]
print(Maux)
我正试着从一个价值列表中绘制一个3d图。所有子列表都有相同数量的值。
我试过这样做:,但是我得到了错误:
ValueError: shape mismatch: objects cannot be broadcast to a single shap
以下是如何复制:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
list_of_lists = [[1,2,3,4,1,2,3,4,1,2,3,4],[2,3,5,9,2,3,5,9,2,3,5,9],[5,9
我有一个int的列表,a,在0到3000之间。len(a) = 3000。我有一个for循环,它迭代这个列表,在一个更大的数组中搜索每个元素的索引。
import numpy as np
a = [i for i in range(3000)]
array = np.random.randint(0, 3000, size(12, 1000, 1000))
newlist = []
for i in range(0, len(a)):
coord = np.where(array == list[i])
newlist.append(coord)
如您所见,coord将是坐
我遵循,但有一个关于将numpy数组转换为张量的问题。教程示例。
这段代码:
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)
收益率
2.2.2.2。
张量(2,2,2,2,2,dtype=torch.float64)
然而,
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
a = a + 1 #the diff is here
print(a)
print(b)
收益率