creating dictionarie names, using variables?

L

Livin

I need to dynamically create dictionary names using strings input at the
time of creation. These will then be placed into a "Parent" dictionary.

I'm new to python, and programming, so please bear with me.

here's my initial thought but I don't think it will work...

item[5]='Kitchen Ceiling Lights'
devDictName = item[5].replace(' ','+')
'dict_'+devDictName = [{'Group':item[2], 'Status':item[3],
'DimLevel':item[4], 'DeviceName':item[5], 'Location':item[6],
'HouseCode':item[7], 'LastChange':item[8], 'DeviceType':item[9],
'CanDim':item[10], 'Values':item[11]}]
dictDevices[item[0],'dict_'+devDictName]


thanks for the help!
 
F

Fredrik Lundh

Livin said:
I need to dynamically create dictionary names using strings input at the
time of creation. These will then be placed into a "Parent" dictionary.

if you think that you need to generate variable names, you're almost
always wrong.
item[5]='Kitchen Ceiling Lights'
devDictName = item[5].replace(' ','+')
'dict_'+devDictName = [{'Group':item[2], 'Status':item[3],
'DimLevel':item[4], 'DeviceName':item[5], 'Location':item[6],
'HouseCode':item[7], 'LastChange':item[8], 'DeviceType':item[9],
'CanDim':item[10], 'Values':item[11]}]
dictDevices[item[0],'dict_'+devDictName]

what's wrong with

dictDevices[item[0]] = {'Group':item[2], 'Status':item[3],
'DimLevel':item[4], 'DeviceName':item[5], 'Location':item[6],
'HouseCode':item[7], 'LastChange':item[8], 'DeviceType':item[9],
'CanDim':item[10], 'Values':item[11]}

</F>
 
D

Dan Sommers

I need to dynamically create dictionary names using strings input at
the time of creation. These will then be placed into a "Parent"
dictionary.

Don't do that.
item[5]='Kitchen Ceiling Lights'
devDictName = item[5].replace(' ','+')
'dict_'+devDictName = [{'Group':item[2], 'Status':item[3],
'DimLevel':item[4], 'DeviceName':item[5], 'Location':item[6],
'HouseCode':item[7], 'LastChange':item[8], 'DeviceType':item[9],
'CanDim':item[10], 'Values':item[11]}]

This is a list that contains one item, and that one item happens to be a
dictionary. Is this really what you want?
dictDevices[item[0],'dict_'+devDictName]

Put the dictionaries you create into another dictionary instead:

devDict = { }

key = item[5].replace(' ','+')

devDict[key] = {'Group':item[2],
'Status':item[3], # etc.
}

HTH,
Dan
 
L

Livin

Are you saying that each child dictionary actually has its own 'key', not
just the items within it?


The goal is to create a dictionary with many dictionaries in it.
- each child dictionary will hold a single 'device' and its related
attributes.
- I want to name the child dictionaries the same as their device name so it
is easy to call them from within the dictionary.
- also, they dictionary will be dynamic, thus the # of devices is always
changing so they need to be created on-the-fly.




Dan Sommers said:
I need to dynamically create dictionary names using strings input at
the time of creation. These will then be placed into a "Parent"
dictionary.

Don't do that.
item[5]='Kitchen Ceiling Lights'
devDictName = item[5].replace(' ','+')
'dict_'+devDictName = [{'Group':item[2], 'Status':item[3],
'DimLevel':item[4], 'DeviceName':item[5], 'Location':item[6],
'HouseCode':item[7], 'LastChange':item[8], 'DeviceType':item[9],
'CanDim':item[10], 'Values':item[11]}]

This is a list that contains one item, and that one item happens to be a
dictionary. Is this really what you want?
dictDevices[item[0],'dict_'+devDictName]

Put the dictionaries you create into another dictionary instead:

devDict = { }

key = item[5].replace(' ','+')

devDict[key] = {'Group':item[2],
'Status':item[3], # etc.
}

HTH,
Dan
 
D

Dennis Lee Bieber

I need to dynamically create dictionary names using strings input at the
time of creation. These will then be placed into a "Parent" dictionary.

Just use the "names" as the key into the "parent" dictionary...
I'm new to python, and programming, so please bear with me.

here's my initial thought but I don't think it will work...

item[5]='Kitchen Ceiling Lights'

So what significance does the LIST named "item" hold? Besides a
bunch of magic indices...
devDictName = item[5].replace(' ','+')
'dict_'+devDictName = [{'Group':item[2], 'Status':item[3],
'DimLevel':item[4], 'DeviceName':item[5], 'Location':item[6],
'HouseCode':item[7], 'LastChange':item[8], 'DeviceType':item[9],
'CanDim':item[10], 'Values':item[11]}]

The [] surrounding that mess means that you are creating a LIST,
inside that list is a single item, a dictionary {}, one entry of which
is called "DeviceName" and holding your original value with spaces
replaced by "+"...
dictDevices[item[0],'dict_'+devDictName]
I'm not sure what this is doing... It doesn't assign a value to
anything, anything it retrieves is dropped on the floor...


-=-=-=-=-=-
masterDict = {}

sampleItem = [ "???", #something
"A" , #group
"OFF", #status
25, #dim level as integer
"Kitchen Ceiling Lights", #device name
"??!", #location
3, #house code
"!??", #last change
4, #device type
"YES", #can dim
"%%" ] #values

# I'm guessing there is a file of items that will be handled in a loop

# unpack the item
(something, group, status, dimLevel, deviceName,
location, houseCode, lastChange, deviceType,
canDim, values) = sampleItem

# create dictionary of attributes -- note: no device name INside
tmp = { "Group" : group,
"Status" : status,
"DimLevel" : dimLevel,
"Location" : location,
"HouseCode" : houseCode,
"LastChange" : lastChange,
"DeviceType" : deviceType,
"CanDim" : canDim,
"Values" : values }

# add attribute dictionary to master dictionary using device name
# as the key
masterDict[deviceName] = tmp
--
 
D

Dan Sommers

Are you saying that each child dictionary actually has its own 'key',
not just the items within it?

I don't quite understand "its own 'key'."

There is a main dictionary. The keys come from replacing spaces with
plusses in item[5]. The values are another dictionary. The keys of
this other dictionary are the strings 'Group', 'Status', etc. The
values of this other dictionary are item[2], item[3], etc.
The goal is to create a dictionary with many dictionaries in it.
- each child dictionary will hold a single 'device' and its related
attributes.

Agreed. Call this the parent dictionary. I called it devDict.
- I want to name the child dictionaries the same as their device name
so it is easy to call them from within the dictionary.

The names of the child dictionaries exist as keys in the parend
dictionary.
- also, they dictionary will be dynamic, thus the # of devices is
always changing so they need to be created on-the-fly.

Yes, child dictionaries can be created on the fly, and be manipulated
and managed within the parent dictionary.

HTH,
Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,276
Messages
2,571,384
Members
48,073
Latest member
ImogenePal

Latest Threads

Top