小编ale*_*wis的帖子

import numpy as np 与 from numpy import

我有一个大量使用的模块numpy

from numpy import array, median, nan, percentile, roll, sqrt, sum, transpose, unique, where
Run Code Online (Sandbox Code Playgroud)

通过使用来保持名称空间清洁是否是更好的做法

import numpy as np
Run Code Online (Sandbox Code Playgroud)

然后当我需要使用时array只需使用np.array, 例如?

这个模块也会被重复调用,比如说几百万次,并且保持命名空间干净似乎会增加一点开销?

setup = '''import numpy as np'''
function = 'x = np.sum(np.array([1,2,3,4,5,6,7,8,9,10]))'
print(min(timeit.Timer(function, setup=setup).repeat(10, 300000)))
Run Code Online (Sandbox Code Playgroud)

1.66832

setup = '''from numpy import arange, array, sum'''
function = 'x = sum(array([1,2,3,4,5,6,7,8,9,10]))'
print(min(timeit.Timer(function, setup=setup).repeat(10, 300000)))
Run Code Online (Sandbox Code Playgroud)

1.65137

为什么使用np.sumvs时会增加更多时间sum

python import numpy

2
推荐指数
1
解决办法
1万
查看次数

是否有一种巧妙的方法将分数表示为属性字符串?

我想知道是否有内置方法来表示

var someFraction = "1/12"
Run Code Online (Sandbox Code Playgroud)

作为属性字符串?即"1"升高和压缩,同时"12"降低并压缩.

谢谢

swift

1
推荐指数
1
解决办法
783
查看次数

iOS(Swift):UITextField是不是成为firstFirstsponder方法后的第一响应者吗?

UITextField在内有一个数组UIViewController

@IBOutlet weak var textField1: UITextField!
@IBOutlet weak var textField2: UITextField!
var textFields: [UITextField] {
    return [textField1, textField2]
}
Run Code Online (Sandbox Code Playgroud)

在我的viewDidLoad方法中,我将textFields数组的第一个元素设置为第一个响应者,并将其所有委托设置如下:

override func viewDidLoad() {
    super.viewDidLoad()

    textFields[0].becomeFirstResponder()
    textFields.forEach({ $0.delegate = self })

    print(textFields.map({ $0.isFirstResponder })) // [false, false]

}
Run Code Online (Sandbox Code Playgroud)

但是,印刷品中的viewDidLoad印刷品声明[false, false]而不是[true, false]我预期的那样。为什么?

谢谢

uitextfield becomefirstresponder ios swift

1
推荐指数
1
解决办法
178
查看次数

iOS(Swift):按下时调暗UIButton

我有一个UIButton生成如下的子类:

class MoreOptionsButton: UIButton {

    override func draw(_ rect: CGRect) {

        let path = UIBezierPath(ovalIn: rect)
        UIColor.red.setFill()
        path.fill()

        let r = CGRect(x: rect.width * 0.5 - rect.width * 0.15 * 0.5, y: rect.height * 0.5 - rect.height * 0.15 * 0.5, width: rect.width * 0.15, height: rect.height * 0.15)
        var circlePath = UIBezierPath(ovalIn: r)
        UIColor.white.setFill()
        circlePath.fill()

        circlePath = UIBezierPath(ovalIn: r.offsetBy(dx: -rect.width / 4.0, dy: 0.0))
        UIColor.white.setFill()
        circlePath.fill()

        circlePath = UIBezierPath(ovalIn: r.offsetBy(dx: rect.width / 4.0, dy: 0.0))
        UIColor.white.setFill()
        circlePath.fill() …
Run Code Online (Sandbox Code Playgroud)

uibutton ios swift

1
推荐指数
1
解决办法
206
查看次数

@IBDesignable UIView未在情节提要中调用init

我有一个UIView@IBDesignable

@IBDesignable
class MyView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        sharedInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        sharedInit()
    }

    private func sharedInit(){
        translatesAutoresizingMaskIntoConstraints = false
        backgroundColor = .blue
    }

}
Run Code Online (Sandbox Code Playgroud)

当我将其放置UIView在情节提要中并MyView在“ 身份”检查器中将其类分配给时,UIView仍然具有默认的背景色。为什么其背景颜色不在UIColor.blue情节提要中?而且,请问我该怎么做呢?

谢谢你的帮助。

ios uistoryboard uibackgroundcolor swift ibdesignable

1
推荐指数
1
解决办法
894
查看次数

iOS:使用Swift使应用程序无响应

有没有办法使应用程序对所有手势,动作等无响应,直到另有指示?

例如,如果一个应用程序响应摇动手势并且有几个按钮,是否有一个全局属性我可以触发,使应用程序对所有这些都没有响应,好吗?

谢谢.

ios swift

1
推荐指数
1
解决办法
74
查看次数

多次暂停和恢复 CAEmitterLayer

我有一个CAEmitterLayer实例,我想暂停然后多次恢复。

我找到了使用两个CAEmitterLayer扩展函数执行此操作的各种方法:

public func pause() {
    speed = 0.0 // Freeze existing cells.
    timeOffset = convertTime(CACurrentMediaTime(), from: self)
    lifetime = 0.0 // Stop creating new cells.
}
Run Code Online (Sandbox Code Playgroud)

public func resume() {
    speed = 1.0
    beginTime = convertTime(CACurrentMediaTime(), from: self) - timeOffset
    timeOffset = 0.0
    lifetime = 1.0
}
Run Code Online (Sandbox Code Playgroud)

第一次使用emitterLayer.pause()emitterLayer.resume()效果很好。

但是,从第二次开始,每当我使用时emitterLayer.pause()emitterCells时间都会稍微向前跳跃。

请问有人可以帮我解决这个跳跃问题吗?

core-animation ios caemitterlayer swift

1
推荐指数
1
解决办法
138
查看次数

iOS(Swift) - UIViewControllers数组

我有一个UIButton具有唯一tag值的数组.当按下给定按钮时,我想加载并呈现一个UIViewController也存储在等长的数组中.

UIViewController要呈现的s是子类的子类UIViewController:

class AViewController: UIViewController {}
class BViewController: AViewController {}
class CViewController: AViewController {}
class DViewController: AViewController {}
// ... etc.
Run Code Online (Sandbox Code Playgroud)

我已经尝试AViewController使用以下内容存储子类数组:

private var array: [AViewController] = [BViewController.self, CViewController.self, DViewController.self]
Run Code Online (Sandbox Code Playgroud)

但我得到错误无法将类型'[BViewController] .Type'的值转换为第一个元素的指定类型'[AViewController]'.

然后,我将BViewController使用以下内容(例如):

let ViewController = array[button.tag].self
var viewController: AViewController
viewController = ViewController.init()
viewController.transitioningDelegate = self
viewController.modalPresentationStyle = .custom
present(viewController, animated: true)
Run Code Online (Sandbox Code Playgroud)

请告诉我,如果这是一个不正确的思考过程,请做这样的事情,并感谢您的帮助.

arrays uiviewcontroller presentmodalviewcontroller ios swift

1
推荐指数
1
解决办法
707
查看次数

如何将列表列表拆分为字典键/值对?

我有以下内容list

is_censored = [[True, True], [False, True], [False, False]]
Run Code Online (Sandbox Code Playgroud)

我想将它放在dictionary与键/值等于is_censored_1 = [True, True]is_censored_2=[False, True]is_censored_3=[False, False]

可以通过Python实现吗?

到目前为止,我有这个:

data = dict()
for i, value in enumerate(is_censored):
    data['_'.join(['is_censored', str(i + 1)])] = value
Run Code Online (Sandbox Code Playgroud)

产生正确的结果:

data = {'is_censored_1': [True, True], 'is_censored_2': [False, True], 'is_censored_3': [False, False]}
Run Code Online (Sandbox Code Playgroud)

但这并不吸引眼球。

python dictionary list

0
推荐指数
1
解决办法
92
查看次数

通过堆叠 html 元素生成一个简单的十字架

我正在尝试在父元素 ( ) 中生成一个简单的交叉(由verticalhorizontal组件组成),但未能成功box

.box {
  width: 50vmin;
  height: 50vmin;
  background-color: blue;
  margin: 0 auto;
}

.vertical {
  position: absolute;
  width: 10%;
  height: 80%;
  top: calc((100% - 80%) / 2);
  left: calc((100% - 10%) / 2);
  background-color: black;
}

.horizontal {
  position: absolute;
  width: 80%;
  height: 10%;
  top: calc((100% - 10%) / 2);
  left: calc((100% - 80%) / 2);
  background-color: black;
}
Run Code Online (Sandbox Code Playgroud)
<div class="box">
  <div class="vertical"></div>
  <div class="horizontal"></div>
</div>
  
Run Code Online (Sandbox Code Playgroud)

我期望的输出是一个以蓝色框为中心的黑色十字。有人可以告诉我我哪里出错了吗?

谢谢你的帮助!

html css absolute

0
推荐指数
1
解决办法
28
查看次数

作为自身实例的类变量

这样的事情可能吗?

class Foo:
    BAR = Foo("bar")
    def __init__(self, name):
        self.name = name
Run Code Online (Sandbox Code Playgroud)

目前这产生NameError: name 'Foo' is not defined.

python variables class

0
推荐指数
1
解决办法
46
查看次数

在哪里放置杂项广泛使用的功能?

这个问题是否有一个简洁的方法来表示一个分数作为属性字符串?.

我有一个为分数字符串提供字体的函数

func fractionFont() -> UIFont {
    let pointSize = CGFloat(20.0)
    let systemFontDescriptor = UIFont.systemFontOfSize(pointSize, weight: UIFontWeightLight).fontDescriptor()
    let fractionFontDescriptor = systemFontDescriptor.fontDescriptorByAddingAttributes(
    [
        UIFontDescriptorFeatureSettingsAttribute: [
        [
            UIFontFeatureTypeIdentifierKey: kFractionsType,
            UIFontFeatureSelectorIdentifierKey: kDiagonalFractionsSelector,
        ], ]
    ] )
    return UIFont(descriptor: fractionFontDescriptor, size: pointSize)
}
Run Code Online (Sandbox Code Playgroud)

我在其中使用了标签属性 SomeClass

class SomeClass {
    @IBOutlet weak var fractionLabel: UILabel! {
        didSet {
            fractionLabel.text = "1/2"
            fractionLabel.font = fractionFont()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

说我想fractionFont再次使用方法AnotherClass我猜复制代码不是一个好主意.我还被告知在另一篇文章中Singleton谨慎地使用所谓的类,那么最好的方法是什么呢?

  • 谢谢

swift

-1
推荐指数
1
解决办法
54
查看次数