lists changed to tuples unexpectedly!

Michael Hudson mwh at python.net
Thu Nov 14 09:49:18 EST 2002


spam at fisher.forestry.uga.edu (Chris Fonnesbeck) writes:

> A very strange thing is occurring to me in Python 2.2.2; I have a module
> that builds a tuple of nested lists like the following:
> 
> # All possible action combinations
> actions = [(((i,0),),((j,j),)) for i in (0,1,2,3,4) for j in
> (0.0,0.1,0.2)]
> 
> I then pass this list as an argument to a class within the module, and it
> magically becomes a tuple of tuples:

This seems unlikely, in the face of it.

> class QOptimizer(ReinforcementLearning.GradientDescentWatkinsQ):
>     'Optimization using Q(Lambda) RL algorithm'
> 
>     def __init__(self,
>                  actions,
>                  states,
>                  init_state,
>                  state_dynamics_function,
>                  objective_function,
>                  rounding = 0,
>                  roundto = 50000,
>                  Alpha = 0.1,
>                  Epsilon = 0.2):
> 
> 	print actions
> 
> 	... etc.
> 
> This gives:
> 
> [[[   [ 0.   0. ]]
>   [   [ 0.   0. ]]]
>  [[   [ 0.   0. ]]
>   [   [ 0.1  0.1]]]
>  [[   [ 0.   0. ]]
>   [   [ 0.2  0.2]]]
>  [[   [ 1.   0. ]]
>   [   [ 0.   0. ]]]
>  [[   [ 1.   0. ]]
>   [   [ 0.1  0.1]]]
>  [[   [ 1.   0. ]]
>   [   [ 0.2  0.2]]]
>  [[   [ 2.   0. ]]
>   [   [ 0.   0. ]]]
>  [[   [ 2.   0. ]]
>   [   [ 0.1  0.1]]]
>  [[   [ 2.   0. ]]
>   [   [ 0.2  0.2]]]
>  [[   [ 3.   0. ]]
>   [   [ 0.   0. ]]]
>  [[   [ 3.   0. ]]
>   [   [ 0.1  0.1]]]
>  [[   [ 3.   0. ]]
>   [   [ 0.2  0.2]]]
>  [[   [ 4.   0. ]]
>   [   [ 0.   0. ]]]
>  [[   [ 4.   0. ]]
>   [   [ 0.1  0.1]]]
>  [[   [ 4.   0. ]]
>   [   [ 0.2  0.2]]]]

That's not the output you get when you print any of the native types
of Python.  It looks a bit like a Numeric.array, though.

> Why did I make lists, and get tuples?

Can you post a complete piece of code showing the problem?

Cheers,
M.

-- 
  The only problem with Microsoft is they just have no taste.
              -- Steve Jobs, (From _Triumph of the Nerds_ PBS special)
                         and quoted by Aahz Maruch on comp.lang.python



More information about the Python-list mailing list