Re: [Tutor] user created lists

2012-04-12 Thread Mark Lawrence

On 12/04/2012 05:42, john moore wrote:

Hello Pyhton World,


Hello Hojn Rooem and welcome :)



I'm new at this and was wondering how I create a number of user specified lists?


Why? Tell us what you're trying to achieve and we may well come up with 
a better solution.




Example:

"How many list would you like to create?"
User inputs 5
creates five lists,
list1 []
list2 []
list3 []
list4 []
list5 []

I can create one with append, but I don't know how to loop it to create five 
different named list..
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor




--
Cheers.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] user created lists

2012-04-12 Thread Alan Gauld

On 12/04/12 08:25, Christian Witts wrote:


I recommend that you bite the bullet and use a dedicated dictionary or list
to hold your five lists from the very begining:

You could use globals() then instead of var(),  although it's a
do-it-at-your-own-risk situation then if you overwrite built-ins and
other functions in the namespace.  Creating your own dictionary with the
'variable name' as the key, would be the nicer solution instead.


Trying to create variables on the fly is usually a bad idea, although it 
seems to appeal to beginners a lot. The problem is: how does the rest of 
the pre-existing code know about these new variable names so as to 
access them.


If you keep the new values in a collection they can be access via a 
loop. But randomly named variables in your code require all sorts of
extra effort to create the name and deference the namespace each time 
you want to access them.


Anonymous values are best kept in anonymous structures IMHO.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] user created lists

2012-04-12 Thread Christian Witts

On 2012/04/12 08:59 AM, Peter Otten wrote:

Christian Witts wrote:


On 2012/04/12 06:42 AM, john moore wrote:

Hello Pyhton World,

I'm new at this and was wondering how I create a number of user specified
lists?

Example:

"How many list would you like to create?"
User inputs 5
creates five lists,
list1 []
list2 []
list3 []
list4 []
list5 []

I can create one with append, but I don't know how to loop it to create
five different named list..



You can use vars() to create the variables on the fly. vars() is just a
dictionary containing the variable name as the key, and the data as the
value so you can do `vars()['list1'] = []` and it's easy enough to
create them en masse

# Set the start to 1, and add 1 to what the user inputted
# as range/xrange doesn't include the top number
for i in xrange(1, user_input + 1):
  vars()['list%s' % i] = []


This will stop working once you move your code into a function:


def f():

... vars()["list1"] = ["a", "b"]
... print list1
...

f()

Traceback (most recent call last):
   File "", line 1, in
   File "", line 3, in f
NameError: global name 'list1' is not defined

I recommend that you bite the bullet and use a dedicated dictionary or list
to hold your five lists from the very begining:
You could use globals() then instead of var(),  although it's a 
do-it-at-your-own-risk situation then if you overwrite built-ins and 
other functions in the namespace.  Creating your own dictionary with the 
'variable name' as the key, would be the nicer solution instead.


--

Christian Witts
Python Developer
//
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] user created lists

2012-04-12 Thread Peter Otten
Christian Witts wrote:

> On 2012/04/12 06:42 AM, john moore wrote:
>> Hello Pyhton World,
>>
>> I'm new at this and was wondering how I create a number of user specified
>> lists?
>>
>> Example:
>>
>> "How many list would you like to create?"
>> User inputs 5
>> creates five lists,
>> list1 []
>> list2 []
>> list3 []
>> list4 []
>> list5 []
>>
>> I can create one with append, but I don't know how to loop it to create
>> five different named list..

> You can use vars() to create the variables on the fly. vars() is just a
> dictionary containing the variable name as the key, and the data as the
> value so you can do `vars()['list1'] = []` and it's easy enough to
> create them en masse
> 
> # Set the start to 1, and add 1 to what the user inputted
> # as range/xrange doesn't include the top number
> for i in xrange(1, user_input + 1):
>  vars()['list%s' % i] = []

This will stop working once you move your code into a function:

>>> def f():
... vars()["list1"] = ["a", "b"]
... print list1
... 
>>> f()
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 3, in f
NameError: global name 'list1' is not defined

I recommend that you bite the bullet and use a dedicated dictionary or list 
to hold your five lists from the very begining:

>>> num_lists = int(raw_input("How many lists? "))
How many lists? 5
>>> list_of_lists = [[] for i in range(num_lists)]

You then have to access the first list as "list_of_list[0]" instead of 
"list1" and so on:

>>> list_of_lists[0].append(1)
>>> list_of_lists[4].append(2)
>>> list_of_lists
[[1], [], [], [], [2]]


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] user created lists

2012-04-11 Thread Christian Witts

On 2012/04/12 06:42 AM, john moore wrote:

Hello Pyhton World,

I'm new at this and was wondering how I create a number of user specified lists?

Example:

"How many list would you like to create?"
User inputs 5
creates five lists,
list1 []
list2 []
list3 []
list4 []
list5 []

I can create one with append, but I don't know how to loop it to create five 
different named list..
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


You can use vars() to create the variables on the fly. vars() is just a 
dictionary containing the variable name as the key, and the data as the 
value so you can do `vars()['list1'] = []` and it's easy enough to 
create them en masse


# Set the start to 1, and add 1 to what the user inputted
# as range/xrange doesn't include the top number
for i in xrange(1, user_input + 1):
vars()['list%s' % i] = []

Hope that helps.
--

Christian Witts
Python Developer
//
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] user created lists

2012-04-11 Thread john moore
Hello Pyhton World,

I'm new at this and was wondering how I create a number of user specified lists?

Example:

"How many list would you like to create?"
User inputs 5
creates five lists,
list1 []
list2 []
list3 []
list4 []
list5 []

I can create one with append, but I don't know how to loop it to create five 
different named list.. 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor