The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Can lists be dynamically named?
Discuss Can lists be dynamically named? in the Python Programming forum on Dev Shed. Can lists be dynamically named? Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 4th, 2013, 12:07 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Location: California, USA
Posts: 6
Time spent in forums: 8 h 3 m 37 sec
Reputation Power: 0
|
|
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.
|

February 4th, 2013, 01:03 PM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
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)
|

February 4th, 2013, 02:10 PM
|
 |
Contributing User
|
|
|
|
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.
|

February 4th, 2013, 07:34 PM
|
|
Contributing User
|
|
Join Date: May 2009
Posts: 297
  
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
|

February 7th, 2013, 02:22 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Location: California, USA
Posts: 6
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|