Numpy.shape有一个后缀:摆脱它的最佳方式

时间:2016-06-21 14:12:14

标签: python numpy

阅读What is the L in numpy.shape and 32 in numpy.type of an array?我知道为什么" L"后缀是有的。但我想做一些涉及了解我的数据集大小的操作。

显然我可以做一些简单的字符串处理来摆脱后缀,但我不想假设它总是在那里(我上次运行代码时并不是这样)不同的机器)。

那么处理这个问题的pythonic方法是什么?我应该使用的是shape()而不是shape()吗?是否有一个简单的"将数字部分转换为整数并抛弃其余部分"我应该在输出上使用的功能?是否有某种方式告诉import Foundation import Parse import UIKit extension UIImageView { public func imageFromUrl(_ urlString: String) { if let url = URL(string: urlString) { let request = URLRequest(url: url) NSURLConnection.sendAsynchronousRequest(request, queue: OperationQueue.main()) { (response: URLResponse?, data: Data?, error: NSError?) -> Void in if let imageData = data as Data? { self.image = UIImage(data: imageData) } } } } } class offersViewController: UIViewController, UITableViewDelegate { @IBOutlet weak var tableView: UITableView! @IBOutlet weak var offersView: UIView! @IBOutlet weak var offerTitleLabel: UILabel! @IBOutlet weak var smartCode: UILabel! @IBOutlet weak var customerName: UILabel! @IBAction func closeButton(sender: AnyObject) { offersView.isHidden = true } var array: [String] = [String]() var arrayImages: [String] = [String]() override func viewDidLoad(){ super.viewDidLoad() } override func viewDidAppear(_ animated: Bool) { self.array.removeAll(keepingCapacity: true) self.arrayImages.removeAll(keepingCapacity: true) let nib = UINib(nibName: "vwTblCell", bundle: nil) tableView.register(nib, forCellReuseIdentifier: "cell") let date = Date() let dateFormatter = DateFormatter() dateFormatter.dateFormat = "EEEE" let weekDay = dateFormatter.string(from: date) print(weekDay) tableView.isHidden = true let query = PFQuery(className:"offers") query.whereKey("day", equalTo:"\(weekDay)") query.findObjectsInBackground { (objects: [PFObject]?, error: NSError?) -> Void in if error == nil { // The find succeeded. print("Successfully retrieved \(objects!.count) scores.") // Do something with the found objects if let objects = objects { let birthdayOffer = "£25 free credit on your Birthday!" let birthdayOfferImage = "birthday" self.array.append(birthdayOffer as String) print(self.array) self.arrayImages.append(birthdayOfferImage as String) print(self.arrayImages) for object in objects { let query = PFQuery(className:"offers") query.getObjectInBackground(withId: "\(object.objectId!)"){ (gameScore: PFObject?, error: NSError?) -> Void in if error == nil && gameScore != nil { let offertitle = gameScore!["offertitle"] as! String let offerImage = gameScore!["imagename"] as! String // Add birthday to array self.array.append(offertitle as String) print(self.array) self.arrayImages.append(offerImage as String) print(self.arrayImages) } self.tableView.reloadData() self.tableView.isHidden = false } } } } } } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return array.count } var cell : UITableViewCell? func tableView(_tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell { let cell: TblCell = self.tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath) as! TblCell let url = URL(string: "https://checkmyweave.co.uk/\(arrayImages[(indexPath as NSIndexPath).row]).png") cell.offerImage.sd_setImage(with: url) cell.offerTitleLabel.text = self.array[(indexPath as NSIndexPath).row] let defaults = UserDefaults.standard() let userID = defaults.string(forKey: "userID")! let query = PFQuery(className:"members") query.getObjectInBackground(withId: "\(userID)") { (gameScore: PFObject?, error: NSError?) -> Void in if error == nil && gameScore != nil { let smartCode = gameScore!["smartcode"] as! String let defaults = UserDefaults.standard() let userName = defaults.string(forKey: "userName")! self.smartCode.text = smartCode self.customerName.text = userName } else { print(error) } } return cell } func numberOfSectionsIntableView(_tableView: UITableView) -> Int { return 1 } private func tableView(_tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print((indexPath as NSIndexPath).row) let date = Date() let dateFormatter = DateFormatter() dateFormatter.dateFormat = "EEEE" let weekDay = dateFormatter.string(from: date) offerTitleLabel.text = self.array[(indexPath as NSIndexPath).row] offersView.isHidden = false print(offerTitleLabel.text) let query = PFQuery(className:"offers") query.whereKey("offertitle", equalTo:"\(offerTitleLabel.text!)") query.whereKey("day", equalTo:"\(weekDay)") query.findObjectsInBackground { (objects: [PFObject]?, error: NSError?) -> Void in if error == nil { // The find succeeded. print("Successfully retrieved \(objects!.count) scores.") // Do something with the found objects if let objects = objects { for object in objects { let query = PFQuery(className:"offers") query.getObjectInBackground(withId: "\(object.objectId!)"){ (gameScore: PFObject?, error: NSError?) -> Void in if error == nil && gameScore != nil { let offerdesc = gameScore!["offerDesc"] as! String print(offerdesc) }}}}}} } private func tableView(_tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 140 } } 我不想要后缀?

奖金问题:这是什么时候介绍/为什么它会出现在一个系统而不是另一个系统上(两个python 2.7,旧系统的numpy版本未知)?在docs中没有提及它,所以没有历史可以帮助我知道我是否可以认为新系统会有它。

1 个答案:

答案 0 :(得分:0)

L是显示值的人工制品,表示该值存储为long,而不是int:它实际上不是部分的价值。因此,你可以忽略它。

>>> num = 123L # create a long for testing
>>> num
123L
>>> num + 1    # simple operations
124L
>>> len(range(num)) # function calls
123
>>> math.sqrt(num) # math module calls also work, most of the time.
11.090536506409418

如果你必须摆脱它,你可以使用int()

>>> int(num) # convert to int
123

唯一可能出现问题的是你有一个非常大的值:

>>> num = 2147483648
>>> num
2147483648L
>>> range(num)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: range() result has too many items

虽然如果你使用range的参数那么大,那么你可能在某个地方的某个地方是unpythonic。