[Tutor] making a random list, then naming it

Dave Angel davea at davea.name
Fri May 24 07:24:11 CEST 2013


On 05/23/2013 10:39 PM, Jim Mooney wrote:
> I keep needing random lists of integers for trying things, so I wrote
> a small prog I'll import as randlist. The user enters the length and
> max val, but then I want the actual data name of the random list
> created, to be   list_(length entered)_(max value entered)  as an easy
> mnemonic.

<snip>

 > print
 > print rlist
 > print
 > print listlenmax
 >

By "name of the list" do you mean like the names rlist and listlenmax 
above?  In other words, you want to define an identifier at run time, 
and bind it to the list object that you have calculated.

And just how would you know you succeeded, unless you already had code 
that happened to use that same identifier?

The short answer is that Python is not designed to be able to do such a 
thing.  You're advised instead to make a dictionary, where the key is 
the name you generate

lists = {}


for .....
     key = 'list' + '_' + str(length) + '_' + str(maxval)
     lists[key] = buildlist(length, maxval)
-- 
DaveA


More information about the Tutor mailing list