python - Basic Variable Indexing -


how do simple index/array in python? example, in traditional basic language, have like

10 dim v(5) 20 n = 1 5:v(n)=2^n+1/n:? n,v(n):next 

which output:

1  3.0 2  4.5 3  8.33333333333 4  16.25 5  32.2 

if wanted weights of gaussian quadrature, do:

ul=5 [x,w] = p_roots(ul) n in range(ul):      print n,w[n] 

which works:

1  0.236926885056 2  0.478628670499 3  0.568888888889 4  0.478628670499 5  0.236926885056 

but if try basic, seem be

ul=5 n in range(1,ul+1):     v[n]=2**n+1.0/n     print n,v[n] 

which rejects as

    v[n]=2**n+1.0/n nameerror: name 'v' not defined 

and if try mimic gaussian example

ul=5 [v]=2**ul+1.0/ul n in range(1,ul+1):     print n,v[n] 

i get

    [v]=2**ul+1.0/ul typeerror: 'float' object not iterable 

in terms of arrays/indexing, isn't v[n] same w[n] (which works in example)? of python documentation seems jump more complicated cases, without providing more rudimentary examples above.

your errors seem clear: in first have not defined v @ (compare basic version used dim v) , in second have assigned v single value (2**ul+1.0/ul), not list of values.

however, should note python lists not arrays, , not size them. instead, compose them go appending. instance, in first version:

ul = 5 v = [] n in range(1,ul+1):     v.append(2**n+1.0/n) 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -