[Tutor] making a random list, then naming it

Jim Mooney cybervigilante at gmail.com
Sat May 25 02:04:32 CEST 2013


> 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


I was probably unclear what I wanted to do. Basically, enter a string of
number pairs of lengths and maxvals to create and print multiple lists, and
name them, so I could fool around with them and not have to remember each
one. I noodled around google and found something to do that.

I haven't done the multiple lists or listing those lists, or input
filtering yet, but I named one list, tested it as a real int list, and it
seems to work. Unless I'm confused ;')

import random
random.seed

listparameters = raw_input('\nEnter two numbers, space separated, for the
length and largest value of a zero-starting random list, which can be used
as variable name: randlist.list_len_max , where len is the list length you
entered, and max is the largest value\n')

listargs = listparameters.split()
rlist = []
length = int(listargs[0])
maxval = int(listargs[1])
for r in range(0,length):
    rlist.append(random.randint(0,maxval))

listlenmax = 'list' + '_' + str(length) + '_' + str(maxval) + ' = rlist'

exec listlenmax

#I entered 20 and 120 just as a test, but any ints will work. The program
will be   changed to list the data names and lists created, accept more
than one number pair so I have multiple lists, and I'll add some input
filters, then put it in jimports

list_20_120[0] += 10042
# test to see if it's a real int list by adding a large number to element
zero

print list_20_120
#this is hard coded but I'll make a list of names taken from input, that I
can use

#result from one run was:
[10095, 98, 110, 89, 86, 2, 51, 88, 36, 20, 85, 84, 98, 20, 11, 64, 17,
111, 22, 5]

#The first number is bigger than the 120 max so it must work.

#the docs on exec were kind of inscrutable. I think some of the docs expect
you to know what you're doing before you read them ;')  But I found some
examples and ran a bunch of monkey-see, monkey-do progs. I haven't really
gotten to dictionaries yet, but this seems to work.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130524/de5da878/attachment.html>


More information about the Tutor mailing list