【发布时间】:2017-05-30 07:52:23
【问题描述】:
这大约是 10 个动机故事,我必须从几个方面对它们进行“评分”。第一个 if 语句检查故事的长度是否超过 280 个字符,第二个 if 语句检查第一个字母是否是大写字母。我想将分数存储在 candidscore 中,所以如果 candid 2 的长度 > 280 并且第一个字母是大写字母,我希望 candidscore[1] 为 2。
代码:
candidscore = numpy.zeros(10)
for i in range(0, 9):
if lengthmot[i] > 280:
candidscore[i] =+ 1
if lengthmot[i] > 0:
if motivation[i][0].isupper():
candidscore[i] =+ 1
问题:
数组candidscore 最初看起来像:array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]) 这样就可以了。
它检查长度是否> 280,这适用于并且数组中有几个。 array([ 1., 0., 1., 1., 1., 0., 1., 0., 1., 0.])。所以这也有效
然后它应该检查第一个字母是否是大写字母,我认为它确实会检查它,但它只会在第一个 if 语句之后仍然为 0 的地方增加分数,所以它看起来像这样:array([ 1., 1., 1., 1., 1., 1., 1., 0., 1., 1.])。
但是根据数据它应该/我希望它看起来像这样:
array([ 2., 1., 2., 2., 1., 1., 1., 0., 1., 2.])。
我不明白为什么已经为 1 的元素不增加。
【问题讨论】:
-
=+不等于+=。
标签: python arrays numpy increment