473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

creating dictionarie names, using variables?

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!
Jan 11 '06 #1
4 1978
"Livin" wrote:
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>

Jan 11 '06 #2
On Wed, 11 Jan 2006 15:53:48 -0700,
"Livin" <li***@cox.net> wrote:
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

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Jan 11 '06 #3
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" <me@privacy.net> wrote in message
news:m2************@unique.fqdn...
On Wed, 11 Jan 2006 15:53:48 -0700,
"Livin" <li***@cox.net> wrote:
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

--
Dan Sommers
<http://www.tombstonezero.net/dan/>

Jan 12 '06 #4
On Wed, 11 Jan 2006 18:00:53 -0700,
"Livin" <li***@cox.net> wrote:
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

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Jan 12 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
20
by: svend | last post by:
I'm messing with some code here... Lets say I have this array: a1 = ; And I apply slice(0) on it, to create a copy: a2 = a1.slice(0); But this isn't a true copy. If I go a1 = 42, and then...
11
by: randi_clausen | last post by:
Using SQL against a DB2 table the 'with' key word is used to dynamically create a temporary table with an SQL statement that is retained for the duration of that SQL statement. What is the...
15
by: James | last post by:
Hi, I am finding it increasingly difficult to name my variables. I am not able to think in the right way. Expert C programmers please Help. Regards,
14
by: Denny | last post by:
For most of my variable names, I use Hungarian notation to determine between one and the other. But what names can I use for public and private variables? I was using prv_varName and pub_varName...
66
by: Mike Meyer | last post by:
It seems that the distinction between tuples and lists has slowly been fading away. What we call "tuple unpacking" works fine with lists on either side of the assignment, and iterators on the...
6
by: Helmut Giese | last post by:
Hello out there, I am new to JS so please bear with me. I am tasked to investigate, whether it is feasible to port one of our applications from Tcl to JS - the idea being to write a program to...
14
by: tdahsu | last post by:
I have twenty-five checkboxes I need to create (don't ask): self.checkbox1 = ... self.checkbox2 = ... .. .. .. self.checkbox25 = ... Right now, my code has 25 lines in it, one for each...
1
by: mk | last post by:
http://www.python.org/dev/peps/pep-0008/ "Function Names Function names should be lowercase, with words separated by underscores as necessary to improve readability." However, this PEP does...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.