小编glw*_*iam的帖子

Python中的列表 - AttributeError:'str'对象没有属性'coeffs'

我在python中列表有问题.

这是简单的代码:

x = [scipy.poly1d([ 1.,  0.,  0.]),2,3,4,5,'foward']
for i in range (len(x)) :
    if x [i] == 'foward':
        print 'check!'
Run Code Online (Sandbox Code Playgroud)

当它运行时它会说:

return NX.alltrue(self.coeffs == other.coeffs)AttributeError:'str'对象没有属性'coeffs'

但当我把x改成:

  x = [1,2,3,4,5,'foward']
Run Code Online (Sandbox Code Playgroud)

该程序将运行没有问题.

有人可以向我解释原因吗?我该怎么办?实际上我有一个数据修复列表(x)返回属性错误,如上所述,我不想改变它的格式和它包含的内容.

python arrays list

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

在python中分类数组列表

我正在帮助我的朋友在python中进行逻辑算法,但我还没有找到最好的解决方案.

首先,我有一个数组列表:

x = array[0,1,2,3,4,3,2,3,-2,-4,-7,2,2]
Run Code Online (Sandbox Code Playgroud)

他想对x进行分类,所以输出变成这样:

array([0,1,2,3,4]) # increasing value
array([4,3,2]) #decreasing value
array([2,3]) # increasing value
array([3,-2,-4,-7]) #decreasing value
array([-7,2]) # increasing value
array([2,2])  # remain_the_same_value
Run Code Online (Sandbox Code Playgroud)

规则很简单:

  1. 如果值继续增加(如上例:0,1,2,3,4),则将它们放入一个数组中
  2. 如果值继续减小(例如上面的例子:3,-2,-4,-7),它们就放在一个数组中
  3. 但是,如果价值模式突然发生变化,例如上面的例子:从增加值(0,1,2,3,4)突然下一个值正在减少.将生成新数组并将最后一个增加的值(即4)监视下一个值,无论它是否为递减值.如果是,它们将被放入一个阵列中.示例:array([4,3,2])
  4. 如果值保持不变(例如上面的例子,从2到2).它们将放在一个阵列中.

这是我到目前为止所做的,但距离解决方案还很远

#categorize which types of input
if len(x) > 2 :
    for i in range(len(x)) :
        if (x[i+1]-x[i]) > 0 and i+i < len(x) : # for increasing x value

        elif (x[i+1]-x[i]) < 0 and i+i < len(x) : # for decreasing x value

        elif (x[i+1]-x[i]) == 0 …
Run Code Online (Sandbox Code Playgroud)

python arrays logic python-2.7

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

标签 统计

arrays ×2

python ×2

list ×1

logic ×1

python-2.7 ×1