如何从稀疏的SciPy矩阵中获得非零值?(How to get non-zero values from sparse SciPy matrix?)
如何获得稀疏矩阵的值? 例如:
x = sp.sparse.csr_matrix([[0,0,-1,1,0],[0,0,0,0,-1]]) print(x) (0, 2) -1 (0, 3) 1 (1, 4) -1
我只是在寻找数据的值,即
[-1, 1, 1]
。How can I get the values of a sparse matrix? For example:
x = sp.sparse.csr_matrix([[0,0,-1,1,0],[0,0,0,0,-1]]) print(x) (0, 2) -1 (0, 3) 1 (1, 4) -1
I am just looking for the values of the data, i.e.,
[-1, 1, 1]
.
原文:https://stackoverflow.com/questions/43617132
最满意答案
Pydude走在正确的轨道上但过早地停了下来。 你得到一个非常无用的错误信息,因为你使用导入为pygame的
from blank import *
方法导入了pygame.mouse
(小m),但是它应该覆盖那个你有mouse = Mouse()
。 问题是你的缩进是错误的。 从running = True
下来的所有内容都需要再次缩进,包括该行。 然后,要使main()
函数运行,您需要在文件末尾包含它:if __name__ == "__main__": main()
所以当你运行代码时发生的事情就是它跳过main()方法(在你的代码中永远不会调用)并在缩进返回后运行这些东西,从
running = True
。 当它到达mouse.display()
你还没有实际运行mouse = Mouse()
,所以它使用从pygame导入的mouse
。 该mouse
没有display()
方法,因此您的错误消息。修复后,您的代码中会出现更多错误,但此更改将使您超过此错误。 下一个至少有一个更有用的错误消息,所以你应该能够弄明白。
此外,一些建议:
from blank import *
因为这个原因而不鼓励。 要么import pygame
并且引用所有内容要好得多,例如pygame.sprite
(你已经在做了......这很奇怪,你正在做两个......)或者from pygame import sprite, foo, bar, ...
做from pygame import sprite, foo, bar, ...
并明确列出您希望从该模块中获得的每个特定事物。 这两种方法可以防止您使未知的东西混乱您的命名空间,并且会阻止您获得您无法弄清楚的这种令人困惑的错误消息。Pydude was on the right track but stopped way too soon. You're getting a very un-helpful error message because
pygame.mouse
(with a small m) is imported when you use thefrom blank import *
method of importing for pygame, but it SHOULD overwrite that one where you havemouse = Mouse()
. The problem is your indentation is wrong. Everything fromrunning = True
down needs to be indented once more, including that line. Then, to get themain()
function to run, you need to include this at the end of your file:if __name__ == "__main__": main()
So what is happening when you run the code as you have it is that it skips right over the main() method (which is never called in your code) and runs the stuff after the indentation goes back, starting with
running = True
. When it gets tomouse.display()
you haven't actually run the linemouse = Mouse()
yet so it usesmouse
imported from pygame. Thatmouse
doesn't have adisplay()
method, hence your error message.You'll have more errors in your code after fixing that, but this change will get you past this one error. The next one has a more helpful error message at least, so you should be able to figure it out.
Also, some advice:
from blank import *
is discouraged for EXACTLY this reason. It's much better to either doimport pygame
and refer to everything as, eg,pygame.sprite
(which you're already doing as well...which is odd that you're doing both...) or dofrom pygame import sprite, foo, bar, ...
and explicitly list each specific thing you want out of that module. These two methods prevent you from having unknown stuff cluttering your namespace, and would have prevented you from getting this confusing error message you couldn't figure out.
相关问答
更多-
找到了解决办法。 def check_collide(self): """Check for a collision with the spikes.""" for spike in self.overlapping_sprites: spike.handle_collide() for循环中的' spike '只是与玩家碰撞的名称。 实际发生的是玩家与“消息”对象发生碰撞,然后询问消息对象处理精灵碰撞的方式。 消息对象未准备好,并且没有' handle_collide ' ...
-
问题似乎是你的for row in cells循环在你之前for x in range(0, self.sizex)循环中。 如果你正确地缩进它,那么step方法应该是什么样子: def step (self): cells = self.cells for x in range (0,self.sizex): if x==0: x_down = self.sizex-1 else: x_down = x-1 if x==self.sizex- ...
-
[pygame错误信息]类型对象'sprite'没有属性'sprites'([pygame error message]type object 'sprite' has no attribute 'sprites')[2022-04-15]
pygame.sprite.spritecollide的文档说参数应该是一组精灵。 它看起来像你创建了一个类, sprite是为了保存该组,但是你将该组分配给本地变量而不是实例成员。 我加了一个self. 到那些,并改变了你得到错误的行,以便它引用它。 我猜这就是你想要的,但不能确切地说明。 class sprite(pygame.sprite.Sprite): def __init__(self, ob, ob1, ob2, image, location): pygame.spr ... -
错误消息:当对象包含属性时,对象不包含属性(Error message: object does not contain attribute when object contains attribute)[2023-01-18]
如果密码有效,则while循环未执行且self.password_str未初始化。 尝试下面的代码: def __init__(self, username_str="-", password_str=""): self.password_str = password_str while (not validated(self.password_str)): self.password_str=input("Password is invalid. Please try a ... -
因此,您似乎正在使用旧的(实际上是非官方的 )Python pywit软件包版本,最新更新于2015-11-07(版本0.4.0) 。 你应该删除pywit包并安装wit ,就像他们在docs / install部分中说的那样: pip uninstall pywit pip install wit 只是为了完整,如果你查看你的旧pywit包的pywit ,在你的python2.7/site-packages/wit/wit.py ,你会看到旧的Wit类的定义,带有get_message()方法而不是当前 ...
-
没有一个叫做'完成'的房间。 当你到达最后它将找不到它。 轻松固定在主循环中。 将游戏更改为: def play(self): current_scene = self.scene_map.opening_scene() while current_scene is not None: print "<<<<<<<<<< MI-Escape from Rogue AI >>>>>>>>>>>>>> " next_scene_name = current_sc ...
-
追逐游戏 - 错误消息:“'模块'对象没有属性'显示'”(Chasing game - Error message: “'module' object has no attribute 'display'”)[2020-02-20]
Pydude走在正确的轨道上但过早地停了下来。 你得到一个非常无用的错误信息,因为你使用导入为pygame的from blank import *方法导入了pygame.mouse (小m),但是它应该覆盖那个你有mouse = Mouse() 。 问题是你的缩进是错误的。 从running = True下来的所有内容都需要再次缩进,包括该行。 然后,要使main()函数运行,您需要在文件末尾包含它: if __name__ == "__main__": main() 所以当你运行代码时发生的事情 ... -
您需要确保正确设置oauth工具: python setup_oauth2client.py install 它在基本目录中 You need to make sure the oauth tools are setup properly: python setup_oauth2client.py install it's in the base directory
-
将变量初始化为声明下方的列表 。 默认情况下,当您第一次为它们分配原始输入时,它们会变成字符串。 做类似的事情: global scores, name, difficulties scores=[] name=[] difficulties=[] 在全球宣言期间。 不需要在函数内再次初始化。 Initialize your variables as lists right below their declaration. By default, when you assign raw input to ...
-
您正在使用socket作为包名称。 它导致导入包socket而不是标准库模块socket 。 重命名它,使其不与标准库模块名称冲突。 You're using socket as a package name. It cause import of your package socket instead of standard library module socket. Rename it so it does not collide with standard library module name.