Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 4th, 2013, 12:07 PM
pyJer pyJer is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Location: California, USA
Posts: 6 pyJer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 3 m 37 sec
Reputation Power: 0
Question Can lists be dynamically named?

I just began studying Python 3.3. Is there a way to dynamically name a set of lists?

My list names will be "lista", "listb", "listc", ... "listz".
I am hoping that the 26 lists can be created and managed using
a loop function with substrings of "abcde...z" to do this.

What I've tried is assigning the string "lista = []" into a variable
x and seeing if eval(x) runs. It displays 'syntax error' with a
caret marker underneath the equal sign.

Reply With Quote
  #2  
Old February 4th, 2013, 01:03 PM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 17 m 59 sec
Reputation Power: 65
Quote:
Originally Posted by pyJer
I just began studying Python 3.3. Is there a way to dynamically name a set of lists?


I think it is possible but the mere fact that it’s difficult should tell you you’re extremely unlikely to ever need it (really, REALLY need it).

Use rather, say, a dictionary of lists: dict_of_lists['a']...dict_of_lists['z'].
__________________
My armada: openSUSE 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)

Reply With Quote
  #3  
Old February 4th, 2013, 02:10 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is online now
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,390 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 15 h 35 sec
Reputation Power: 383
You can create python expressions as strings and then call eval(my_string,globals,locals) to evaluate them. Assignments are statements so you'd need exec(my_string,other_arguments_to_exec) . I absolutely agree with SuperOscar that you need to learn more ways to express algorithms with python instead of dynamically creating variable names.

oh my goodness I didn't realize this:
Code:
>>> no_such_variable
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'no_such_variable' is not defined
>>> 
>>> globals()['no_such_variable'] = 'Now I have a value.'
>>> 
>>> no_such_variable
'Now I have a value.'
>>> 
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : February 4th, 2013 at 02:15 PM.

Reply With Quote
  #4  
Old February 4th, 2013, 07:34 PM
dwblas dwblas is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 297 dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 19 h 28 m 48 sec
Reputation Power: 7
Generally, a dictionary is used but as stated above, you don't really need it. State the problem that you are trying to solve as there is possibly a better way to do it.
Code:
dict_of_lists = {}
ctr = 97  ## a unique number to identify each list
for x in range(10):
    dict_of_lists[chr(ctr)] = [ctr]
    ctr += 1
print dict_of_lists 

Reply With Quote
  #5  
Old February 7th, 2013, 02:22 PM
pyJer pyJer is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Location: California, USA
Posts: 6 pyJer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 3 m 37 sec
Reputation Power: 0
Thank you for those responses, and thanks for steering me away from many lists when few will do.
I'm doing this just to become proficient with Python and I am currently at the beginning where all results are viewed in a DOS window or the IDLE window.
I have a list of 700+ mp3 files in a folder. My first idea was to store all file names starting with a letter of the alphabet in separate lists, plus a list of file names starting with a non-letter, to be able to present each list separately, with each item numbed (1-700), and displayed in dual columns. So when 'c' is entered at the prompt, 2 columns of only files starting with 'C', perhaps numbered 110 thru 170. I will type in a number and the number will be saved for subsequent writing of an mp3 playlist.
My current plan now is to store the entire list in one list object, formatted in dual columns, just the way it will appear on the screen. Another list object contains mapping of the main list: it tells me where each group begins in the main list, and how many items in the group. Then when I navigate to a letter, I take the values from the mapping list and get the correct items from the main list.
At this early stage, I can display a single group in dual-column format. Bottom line, I am satisfied with how the project is progressing.

Note. This may be obvious, but all of this is done in code. The main list is analyzed in code to create the mapping list.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Can lists be dynamically named?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap