访问对象数据切片numpy数组

时间:2016-04-15 14:00:03

标签: python arrays numpy optimization

所以我正在使用numpy数组而不是每个循环来优化一些python代码。在切片numpy数组期间有没有办法从类中访问字段?

protocol AMPlayerItemDelegate {
    func playbackLikelyToKeepUp()
}

class AMPlayerItem: AVPlayerItem {

   var delegate : MyPlayerItemDelegate?

   init(URL: NSURL) {
       super.init(asset: AVAsset(URL: URL) , automaticallyLoadedAssetKeys:[])
       self.addMyObservers()
   }

   deinit {
       self.removeMyObservers()
   }

   func addMyObservers() {
       print("Adding")
       self.addObserver(self, forKeyPath: "playbackLikelyToKeepUp", options: [.New], context: nil)
   }

   func removeMyObservers() {
       print("Removing")
       self.removeObserver(self, forKeyPath: "playbackLikelyToKeepUp", context: nil)
   }

   override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
       if keyPath == "playbackLikelyToKeepUp" {
           self.delegate?.playbackLikelyToKeepUp()
       }
   }

}

如果以这种方式不可能,我将如何有效地生成我的&#34; int_array&#34;变量而不使用for循环遍历&#34; ar&#34;,收集&#34; the_int&#34;从每个条目?

谢谢, 凯尔

1 个答案:

答案 0 :(得分:2)

为什么使用numpy数组来存储PyObjects?你不会得到你认为的性能提升。请参阅here

使用列表,您可以使用列表推导:

class foo:

    the_int = 0

    def __init__(self, i):
        self.bar = i

one, two = foo(5), foo(10)

ar = [one, two]

int_array = [i.bar for i in ar[0:2]]