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.
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):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With