首页 \ 问答 \ 如何从稀疏的SciPy矩阵中获得非零值?(How to get non-zero values from sparse SciPy matrix?)

如何从稀疏的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
更新时间:2022-07-20 06:07

最满意答案

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 the from blank import * method of importing for pygame, but it SHOULD overwrite that one where you have mouse = Mouse(). The problem is your indentation is wrong. Everything from running = True down needs to be indented once more, including that line. Then, to get the main() 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 to mouse.display() you haven't actually run the line mouse = Mouse() yet so it uses mouse imported from pygame. That mouse doesn't have a display() 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 do import 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 do from 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.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。