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(returnedlis t):
# then create a dictionary (beginning with variable dic) for each i
with a unique name such that
# my unique name would be dic + count
Any ideas about this?
Greg
--
View this message in context: http://www.nabble.com/Creating-Uniqu....html#a9943317
Sent from the Python - python-list mailing list archive at Nabble.com. 5 2876
Greg Corradini a écrit :
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(returnedlis t):
# then create a dictionary (beginning with variable dic) for each i
with a unique name such that
# my unique name would be dic + count
Any ideas about this?
Yes : use a dict to store your dicts:
returnedlist = [x,y,z]
dicts = dict()
for num, item in enumerate(retur nedlist):
dicts['dict%s' % num] = dict()
Bruno,
Your help is much appreciated. I will give this a try tomorrow morning and
get back on how it works.
Bruno Desthuilliers wrote:
Greg Corradini a écrit :
>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(returnedlis t): # then create a dictionary (beginning with variable dic) for each i with a unique name such that # my unique name would be dic + count
Any ideas about this?
Yes : use a dict to store your dicts:
returnedlist = [x,y,z]
dicts = dict()
for num, item in enumerate(retur nedlist):
dicts['dict%s' % num] = dict()
-- http://mail.python.org/mailman/listinfo/python-list
--
View this message in context: http://www.nabble.com/Creating-Uniqu....html#a9947284
Sent from the Python - python-list mailing list archive at Nabble.com.
Greg Corradini a écrit :
Bruno,
Your help is much appreciated.
Then give thanks to Dennis too !-)
I will give this a try tomorrow morning and
get back on how it works.
Don't worry, it just works - and it's the idiomatic solution to the
problem you described.
On Wed, 11 Apr 2007 21:03:20 +0200, Bruno Desthuilliers wrote:
Greg Corradini a écrit :
>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(returnedlis t): # then create a dictionary (beginning with variable dic) for each i with a unique name such that # my unique name would be dic + count
Any ideas about this?
Yes : use a dict to store your dicts:
returnedlist = [x,y,z]
dicts = dict()
for num, item in enumerate(retur nedlist):
dicts['dict%s' % num] = dict()
Given that num is unique each time around the loop, what do you gain by
using 'dictN' for the key instead of just N (=num)?
returnedlist = [x,y,z]
dicts = {}
for num, item in enumerate(retur nedlist):
# presumably you would use item somewhere
dicts[num] = {item: None}
And that suggests that storing the dicts in a dict may be unnecessary --
just put them in a list:
returnedlist = [x,y,z]
dicts = [None] * len(returnedlis t)
for num, item in enumerate(retur nedlist):
dicts[num] = {item: None}
--
Steven.
Steven D'Aprano a écrit :
On Wed, 11 Apr 2007 21:03:20 +0200, Bruno Desthuilliers wrote:
>>Greg Corradini a écrit :
>>>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:
returnedli st = [x,y,z] count = 0 for i in returnedlist: if count < len(returnedlis t): # then create a dictionary (beginning with variable dic) for each i with a unique name such that # my unique name would be dic + count
Any ideas about this?
Yes : use a dict to store your dicts:
returnedlis t = [x,y,z] dicts = dict() for num, item in enumerate(retur nedlist): dicts['dict%s' % num] = dict()
Given that num is unique each time around the loop, what do you gain by
using 'dictN' for the key instead of just N (=num)?
The OP wanted such names, that's all. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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* just
created a new pointer to the original value....
For example I wanted to initialize a list of empty lists....
a=, , , , ]
I thought there has to be a *really* easy way of doing it - after a
|
by: kevin parks |
last post by:
hi. I've been banging my head against this one a while and have asked
around, and i
am throwing this one out there in the hopes that some one can shed
some light on
what has turned out to be a tough problem for me (though i am getting
closer).
i have been mucking with a lot of data in a dictionary that looks
like:
|
by: Livin |
last post by:
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='Kitchen Ceiling Lights'
devDictName = item.replace(' ','+')
'dict_'+devDictName = , 'Status':item,
|
by: Danny Liberty |
last post by:
I need some opionions on an issue here...
Suppose I want to keep a collection of objects, each need to be
uniquely identified by a number. This number has no meaning as long as
it's unique, so it should be automatically generated for each new
object. One more requirement is keeping the objects in the order in
which they were inserted, not sorted by their ID.
Considering the following options of implementation, which would be the
best...
|
by: blisspikle |
last post by:
I figure that someone good at dotnet can look at this and give me a
clue on how to easily organize this code?
If there is a unique identifier like "Publisher" with a bunch of
"Book" that are published under them (I used the arraylist class in
the publisher class). How should the code be organized, and how can
the books properties like "Name" be easily called in the main code, or
searched for in the main code?
| |
by: Greg Corradini |
last post by:
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 =
count = 0
for i in returnedlist:
if count < len(returnedlist):
|
by: =?Utf-8?B?bWFnZWxsYW4=?= |
last post by:
Hi,
I'd appreciae any advice on how to do this in VB 2005.
I have a simple array;e..g, a list of States, e.g., CA, WA, ID, AL, and
etc...
I like to determine how many unqiue "States" are in this array...ie.,
duplicate entires, are igorned...
|
by: Gabriel Genellina |
last post by:
En Fri, 18 Apr 2008 12:23:08 -0300, Shawn Milochik <Shawn@Milochik.comescribió:
A dictionary with keys is perfectly reasonable. But a *list* of values has to be searched linearly for every value: a O(n) process. As your friend suggested, searching a dictionary requires O(1) time. A set is even better in this case, because you don't have any use for the values in the inner dictionary (sets and dictionaries are very similar in the...
|
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 checkbox, since
|
by: =?Utf-8?B?UGF1bA==?= |
last post by:
Hi I have a list of type object. The object has an ID as one of the elements
and I would like to create another list that just has objects with unique IDs.
For example
in the list if I have
listofobject.ID = 1
listofobject.ID =1
listofobject.ID = 2
I would like new list with only
listofobject.ID =1
listofobject.ID = 2
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |