我对以下代码输出感到困惑:
import numpy as np
numStates = 4
v1 = np.zeros( numStates, dtype = float)
v2 = np.zeros( numStates, dtype = float )
d1 = np.array([0.24, 0.16, 0.08, 0.8])
d2 = np.array([0.56, 0.64, 0.72, 0.0])
payoff = np.array( [ 0.0, 10.0, 20.0, 30.0 ] )
# to index interior point
indexVet = np.arange(1, 3, 1)
stopit = False
maxIter = 10
count = 0
while not stopit:
count = count + 1
print('Before update v2[0]')
print('v2' + str(v2))
print('v1' + str(v1))
v2[0] = d1[0] * v1[0] + d2[0] * v1[1]
print('After update v2[0]')
print('v2' + str(v2))
print('v1' + str(v1))
valueFlush = payoff[indexVet] + v1[0]
valueWait = d1[indexVet] * v1[indexVet] + d2[indexVet] * v1[indexVet + 1]
v2[indexVet] = np.maximum( valueFlush, valueWait )
v2[numStates - 1] = payoff[numStates-1] + v1[0]
if ( ( np.max( v2 - v1 ) < 0.0001 ) or ( count > maxIter ) ):
stopit = True
else:
v1 = v2
这里我尝试使用“while not”来控制我的循环,然后我从输出中看到的是当我执行“v2 [0] = d1 [0] * v1 [0] + d2 [0]行时* v1 [1]“,v2值自动进入v1,就像命令”v1 = v2“被隐式执行一样。我尝试重命名变量,而不是帮助。在执行“v2 [0] = d1 [0] * v1 [0] + d2 [0] * v1 [1]”后,任何人都知道为什么v2等于v1?谢谢!
答案 0 :(得分:0)
你的缩进不正确
private func descriptionAttributedText() -> NSAttributedString {
let title = "some title on the left side of the screen"
let subtitle = "subtitle on the right side"
let rightParagraph = NSMutableParagraphStyle()
rightParagraph.alignment = .right
let titleAttributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14), NSAttributedStringKey.foregroundColor: UIColor.black]
let titleString = NSMutableAttributedString(string: title, attributes: titleAttributes)
let subtitleAttributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 13), NSAttributedStringKey.foregroundColor: UIColor.darkGray, NSAttributedStringKey.paragraphStyle: rightParagraph]
let subtitleString = NSAttributedString(string: subtitle, attributes: subtitleAttributes)
let paragraph = NSMutableParagraphStyle()
paragraph.setParagraphStyle(.default)
paragraph.lineSpacing = 2
let range = NSMakeRange(0, titleString.string.count)
titleString.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraph, range: range)
titleString.append(subtitleString)
return titleString
}
应该与while排队,而不是if