Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Creating Unique Dictionary Variables from List

Reply
Thread Tools

Creating Unique Dictionary Variables from List

 
 
Greg Corradini
Guest
Posts: n/a
 
      04-11-2007

Hello All,
I'm attempting to create multiple dictionaries at once, each with unique
variable names. The number of dictionaries i need to create depends on the
length of a list, which was returned from a previous function.
The pseudo code for this problem would be:

returnedlist = [x,y,z]
count = 0
for i in returnedlist:
if count < len(returnedlist):
# then create a dictionary (beginning with variable dic) and add a
unique ending such that
# my final dictionary name would be dic + count for each i

Any ideas about this?
Greg
--
View this message in context: http://www.nabble.com/Creating-Uniqu....html#a9943321
Sent from the Python - python-list mailing list archive at Nabble.com.

 
Reply With Quote
 
 
 
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      04-11-2007
On Wed, 11 Apr 2007 09:57:35 -0700 (PDT), Greg Corradini
<(E-Mail Removed)> declaimed the following in comp.lang.python:

>
> Hello All,
> I'm attempting to create multiple dictionaries at once, each with unique
> variable names. The number of dictionaries i need to create depends on the
> length of a list, which was returned from a previous function.
> The pseudo code for this problem would be:

<snip>
> Any ideas about this?


Besides not posting three copies of the request? <G>

The simplest scheme is to use a (master) dictionary.

mDict = {}

for c, d in enumerate(aList):
mDict["dic_%s" % c] = {}
#do something with the data in d?
--
Wulfraed Dennis Lee Bieber KD6MOG
http://www.velocityreviews.com/forums/(E-Mail Removed) (E-Mail Removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (E-Mail Removed))
HTTP://www.bestiaria.com/
 
Reply With Quote
 
 
 
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      04-11-2007
On Wed, 11 Apr 2007 17:20:08 GMT, Dennis Lee Bieber
<(E-Mail Removed)> declaimed the following in comp.lang.python:

Talking to myself?

>
> The simplest scheme is to use a (master) dictionary.
>
> mDict = {}
>
> for c, d in enumerate(aList):
> mDict["dic_%s" % c] = {}
> #do something with the data in d?


Unmentioned is that, if the name is just some constant with the
ordinal position from the list... why bother? Which would you rather
see/code:

#using random to represent some means of arbitrary access
for i in random.randint(0, len(mDict)-1):
key = "This_is_the_dictionary_of_list_item_number_%s " % i
something = mDict[key]["something"]

or the simplicity of using a list to begin with:

theDicts = []

for c, d in enumerate(aList):
theDicts.append({})
#do something with the data in d, referencing theDicts[c]

for i in random.randint(0, len(theDicts)-1):
something=theDicts[i]["something"]


Now... If the "name" of the "variable" were based upon, say, the
first subelement of each list entry... Then the mDict model makes sense:

for d in aList:
mDict[d[0]] = d[1:] #or whatever is needed to populate the sub
dictionary
--
Wulfraed Dennis Lee Bieber KD6MOG
(E-Mail Removed) (E-Mail Removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (E-Mail Removed))
HTTP://www.bestiaria.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a unique method in python to unique a list? Token Type Python 9 09-09-2012 02:13 PM
list question... unique values in all possible unique spots ToshiBoy Python 6 08-12-2008 05:01 AM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
Creating Unique Dictionary Variables from List Greg Corradini Python 5 04-12-2007 08:09 PM
Creating Unique Dictionary Variables from List Greg Corradini Python 0 04-11-2007 04:53 PM



Advertisments