Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lists in Python - AttributeError: 'str' object has no attribute 'coeffs'

I have problem with list in python.

here is the simple codes:

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

when it's run it will say:

return NX.alltrue(self.coeffs == other.coeffs) AttributeError: 'str' object has no attribute 'coeffs'

but when i change the x into :

  x = [1,2,3,4,5,'foward']

the program will run no problem.

is there someone could explain to me why? and what should i do? actually i have a fix list of data (x) which return attribute error like above and i don't want to change the format of it and what its contain.

like image 364
glwilliam Avatar asked Dec 31 '25 22:12

glwilliam


1 Answers

if isinstance(x[i], basestring) and x[i] == 'forward'

or quick-and-dirty:

if str(x[i]) == 'forward'

You should also use the for .. in loop to iterate over the list:

for elem in x:
    if isinstance(elem, basestring) and elem == 'forward':
        print 'Check'

If you need i, too:

for i, elem in enumerate(x):
like image 57
ThiefMaster Avatar answered Jan 03 '26 13:01

ThiefMaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!