我有一个这个结构的2d坐标列表:
coo = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0)]
Run Code Online (Sandbox Code Playgroud)
coo[0]存储在元组中的第一个坐标在哪里.
我想选择两个不同的随机坐标.我当然可以使用这种方法:
import numpy as np
rndcoo1 = coo[np.random.randint(0,len(coo))]
rndcoo2 = coo[np.random.randint(0,len(coo))]
if rndcoo1 != rndcoo2:
#do something
Run Code Online (Sandbox Code Playgroud)
但是因为我必须重复这个操作1'000'000次,我想知道是否有更快的方法来做到这一点.np.random.choice()不能用于2d阵列有没有我可以使用的替代方案?
我正在尝试重载一个运算符,强制它返回当前类的同一实例的对象,而不是方法重载的父类.
class Book:
def __init__(self,name,pages):
self.name=name
self.pages=pages
def __add__(self,other):
return Book(self.name,(self.pages + other.pages))
class Encyclopedia(Book):
def __init__(self,name,pages):
Book.__init__(self,name,pages)
a=Encyclopedia('Omina',234)
b=Encyclopedia('Omnia2',244)
ab=a+b
print ab
Out: <__main__.Book instance at 0x1046dfd88>
Run Code Online (Sandbox Code Playgroud)
例如,在这种情况下,我想返回一个Encycolpedia实例(不是一个Book实例)而不会重载另一个运算符__add__与同一行 Encyclopedia而不是Book我尝试过:
return self(self.name,(self.pages + other.pages))
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
如果Class Enclcopedia有另一个属性怎么办:
class Encyclopedia(Book):
def __init__(self,name,pages,color):
Book.__init__(self,name,pages)
self.color=color
Run Code Online (Sandbox Code Playgroud) 我找不到更改使用 matplotlib GUI 保存的文件的默认 DPI 来显示绘图的方法:
当我使用图标保存图形时,DPI 被默认设置。通常,我使用: import matplotlib.pyplot as plt
plt.savefig(figurename,dpi=1000)
Run Code Online (Sandbox Code Playgroud)
我得到了预期的输出,但我无法从 GUI 中保存图形获得相同的输出。
我正在寻找执行两个或多个数字的平方和的平方根的更有效和最短的方法。我实际上正在使用numpy这个代码:
np.sqrt(i**2+j**2)
Run Code Online (Sandbox Code Playgroud)
这似乎比:
np.sqrt(sum(np.square([i,j])))
Run Code Online (Sandbox Code Playgroud)
(i 和 j 是数字!)
我想知道是否已经有一个更有效的内置函数可以用更少的代码来执行这个非常常见的任务。
我试图找到一个解决方案来展平以下 numpy 数组列表:
a = np.arange(9).reshape(3,3)
b = np.arange(25).reshape(5,5)
c = np.arange(4).reshape(2,2)
myarrs = [a,b,c]
d = np.arange(5*5*5).reshape(5,5,5)
myarrs2 = [a,b,c,d]
Run Code Online (Sandbox Code Playgroud)
对于我myarrs目前正在使用的:
res = np.hstack([np.hstack(i) for i in myarrs])
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有任何其他内置方法可以执行此任务,特别是在具有不同形状的数组的情况下。我看到了其他问题: Flattening a list of NumPy arrays? 但它们通常指的是具有相同形状的数组。
我正在使用 np.arrays。我试图删除最后 n 个元素,其中 n 也可以是 1。
n=5
corr=np.full(10,10)
Run Code Online (Sandbox Code Playgroud)
通常我使用这种方法进行数组切片:
corr=corr[:-n]
Run Code Online (Sandbox Code Playgroud)
但我正在考虑使用 np.delete 来提高性能:
np.delete(corr,range(-n,0))
Run Code Online (Sandbox Code Playgroud)
但是不行,有没有比数组切片更好的解决方案?(该方法还能够处理 n=0 的情况,这将是一个优点)
我想以instance's attributes 编程方式编译从外部(.csv)文件导入数据.到目前为止,我可以手动执行一个实例.使用此工作流程:
class RS: #the calss has the importer method and many attributes
...
#workflow starts here
a=RS() #I create the instance
a.importer('pathofthefile') #the importer method fills the attributes of the instance with the exeternal file
#ends here and restart...
b=RS()
b.importer('path...
Run Code Online (Sandbox Code Playgroud)
我想以编程方式创建实例并填充它们importer.如何class在大量文件上迭代此过程?例如,listdir用于导入文件夹中的所有文件?我喜欢这样的东西来创建实例:
for i in 'abcd':
eval('%s=RS()' %(i))
Run Code Online (Sandbox Code Playgroud)
但当然似乎不起作用..
我试图像这样计算False价值np.array:
import numpy as np
a = np.array([[True,True,True],[True,True,True],[True,False,False]])
Run Code Online (Sandbox Code Playgroud)
我通常使用这种方法:
number_of_false=np.size(a)-np.sum(a)
Run Code Online (Sandbox Code Playgroud)
有没有更好的办法?
python ×8
numpy ×5
arrays ×2
class ×2
performance ×2
boolean ×1
inheritance ×1
matplotlib ×1
random ×1