Hello...
I have set up a dictionary into whose values I am putting a list. I
loop around and around filling my list each time with new values, then
dumping this list into the dictionary. Or so I thought...
It would appear that what I am dumping into the dictionary value is
only a pointer to the original list, so after all my iterations all I
have is a dictionary whose every value is equal to that of the list the
final time I looped around :-(
Is there a way to acheive what I was attempting ? I have done something
almost identical with classes in a list before, and in that case a new
instance was created for each list entry...
I hope this makes some sense, and doesn't seem to head bangingly
simple...
Cheers,
Ben 10 2340
Ben wrote:
Hello...
I have set up a dictionary into whose values I am putting a list. I
loop around and around filling my list each time with new values, then
dumping this list into the dictionary. Or so I thought...
It would appear that what I am dumping into the dictionary value is
only a pointer to the original list, so after all my iterations all I
have is a dictionary whose every value is equal to that of the list the
final time I looped around :-(
Is there a way to acheive what I was attempting ? I have done something
almost identical with classes in a list before, and in that case a new
instance was created for each list entry...
I hope this makes some sense, and doesn't seem to head bangingly
simple...
Do you consult your physician over a video link while wearing a ninja
costume down an unlit coal mine at midnight?
Please consider the possibility that your description of what you think
your code might be doing is not enough for diagnosis.
You may need to supply:
(1) a listing of your code
(2) a small amount of input data
e.g. [(1, 'foo'), (42, 'bar'), (1, 'zot')]
(3) the output you expect from that input:
e.g. {1: ['foo', 'zot'], 42: ['bar']}
Cheers,
John
"Ben" <Be************ *@gmail.comwrit es:
I have set up a dictionary into whose values I am putting a list. I
loop around and around filling my list each time with new values,
then dumping this list into the dictionary. Or so I thought...
Our crystal balls are notoriously unreliable for viewing program code
that hasn't been posted.
--
\ "For man, as for flower and beast and bird, the supreme triumph |
`\ is to be most vividly, most perfectly alive" -- D.H. Lawrence. |
_o__) |
Ben Finney
Ben wrote:
I have set up a dictionary into whose values I am putting a list. I
loop around and around filling my list each time with new values, then
dumping this list into the dictionary. Or so I thought...
It would appear that what I am dumping into the dictionary value is
only a pointer to the original list, so after all my iterations all I
have is a dictionary whose every value is equal to that of the list the
final time I looped around :-(
Is there a way to acheive what I was attempting ?
Where you "loop around ... filling [your] list", use a new
list every time. You can create a new empty list with "[]".
--
--Bryan
On 6 Oct 2006 14:37:59 -0700, Ben <Be************ *@gmail.comwrot e:
Is there a way to acheive what I was attempting ? I have done something
almost identical with classes in a list before, and in that case a new
instance was created for each list entry...
Not sure what you're trying to pull off, but you may wish to copy the
items in question. (Questions indeed!). Dictionarys have their own
shallow copy method, surprisingly named copy, and there is also a copy
module that does shallow and deep copy (copy and deepcopy, resp.)
HTH,
Theerasak
I think what you mean is that if you change your list, it is changed
somewhere in your dicrionary to. Lists are always copied as pointers,
except explicitly told other wise. So a = b = [] makes a and be the
same list, and a.append(1) makes b - [1].
So do something like mydict[mykey] = mylist[:] (Slicing gives a copy
of the list, not the pointer).
Hope this helps.
Moi
Dolf
On 6 Oct 2006 14:37:59 -0700, "Ben" <Be************ *@gmail.comwrot e:
>Hello...
I have set up a dictionary into whose values I am putting a list. I loop around and around filling my list each time with new values, then dumping this list into the dictionary. Or so I thought...
It would appear that what I am dumping into the dictionary value is only a pointer to the original list, so after all my iterations all I have is a dictionary whose every value is equal to that of the list the final time I looped around :-(
Is there a way to acheive what I was attempting ? I have done something almost identical with classes in a list before, and in that case a new instance was created for each list entry...
I hope this makes some sense, and doesn't seem to head bangingly simple...
Cheers,
Ben
John Machin wrote:
Ben wrote:
>>Hello...
I have set up a dictionary into whose values I am putting a list. I loop around and around filling my list each time with new values, then dumping this list into the dictionary. Or so I thought...
It would appear that what I am dumping into the dictionary value is only a pointer to the original list, so after all my iterations all I have is a dictionary whose every value is equal to that of the list the final time I looped around :-(
Is there a way to acheive what I was attempting ? I have done something almost identical with classes in a list before, and in that case a new instance was created for each list entry...
I hope this makes some sense, and doesn't seem to head bangingly simple...
Do you consult your physician over a video link while wearing a ninja
costume down an unlit coal mine at midnight?
Please consider the possibility that your description of what you think
your code might be doing is not enough for diagnosis.
You may need to supply:
(1) a listing of your code
(2) a small amount of input data
e.g. [(1, 'foo'), (42, 'bar'), (1, 'zot')]
(3) the output you expect from that input:
e.g. {1: ['foo', 'zot'], 42: ['bar']}
One of the fascinating things about c.l.py is that sometimes a questin
will be posted that makes almost no sense to me, and somebody else will
casually read the OP's mind, home in on the issue and provide a useful
and relevant answer.
In this case it seems transparent to me, though probably not to you,
that Ben's problem is rootd in the following behaviour, well-known in
python but frequently confusing to noobs:
>>a = [1, 2, 3] firstlist = a a.append('ano ther element') firstlist
[1, 2, 3, 'another element']
>>>
Ben probably needs to look at creating copies using the list() type:
>>a = [1, 2, 3] firstlist = list(a) a.append('ano ther element') firstlist
[1, 2, 3]
>>>
or perhaps, in omore complex circumstances, using the copy module.
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden
Steve Holden wrote:
One of the fascinating things about c.l.py is that sometimes a questin
will be posted that makes almost no sense to me, and somebody else will
casually read the OP's mind, home in on the issue and provide a useful
and relevant answer.
if the assertions made by some about the documentation's unsuit-
ability for some are in fact true, that's probably some kind of
natural selection in action.
</F>
Steve Holden wrote:
John Machin wrote:
Ben wrote:
>Hello...
I have set up a dictionary into whose values I am putting a list. I loop around and around filling my list each time with new values, then dumping this list into the dictionary. Or so I thought...
It would appear that what I am dumping into the dictionary value is only a pointer to the original list, so after all my iterations all I have is a dictionary whose every value is equal to that of the list the final time I looped around :-(
Is there a way to acheive what I was attempting ? I have done something almost identical with classes in a list before, and in that case a new instance was created for each list entry...
I hope this makes some sense, and doesn't seem to head bangingly simple...
Do you consult your physician over a video link while wearing a ninja
costume down an unlit coal mine at midnight?
Please consider the possibility that your description of what you think
your code might be doing is not enough for diagnosis.
You may need to supply:
(1) a listing of your code
(2) a small amount of input data
e.g. [(1, 'foo'), (42, 'bar'), (1, 'zot')]
(3) the output you expect from that input:
e.g. {1: ['foo', 'zot'], 42: ['bar']}
One of the fascinating things about c.l.py is that sometimes a questin
will be posted that makes almost no sense to me, and somebody else will
casually read the OP's mind, home in on the issue and provide a useful
and relevant answer.
In this case it seems transparent to me, though probably not to you,
that Ben's problem is rootd in the following behaviour, well-known in
python but frequently confusing to noobs:
>>a = [1, 2, 3]
>>firstlist = a
>>a.append('ano ther element')
>>firstlist
[1, 2, 3, 'another element']
>>>
It's quite transparent to me that his symptom is caused by the one list
being used throughout the exercise, instead of one per different dict
key. What you have described is one possibility.
Here's another possibility: Making the charitable assumption that he
has an outer loop and an inner loop, maybe (as I think another poster
has already suggested) all he needs to do is move "mylist = []" inside
the outer loop. Note that he doesn't say explicitly whether the one
list that he gets is the *correct* list for the last key, or whether
it's the catenation of all the correct lists, or something else.
Yet another: Noobs do all sorts of funny things. He could be operating
on a "clean the bucket out after each use instead making a new one"
paradigm:
| >>d= {}
| >>L = []
| >>L.append(1)
| >>L.append(2)
| >>d['a'] = L
| >>d
| {'a': [1, 2]}
| >>del L[:]
| >>d
| {'a': []}
| >>L.append(3)
| >>L.append(4)
| >>d['b'] = L
| >>d
| {'a': [3, 4], 'b': [3, 4]}
Cheers,
John
I think what you mean is that if you change your list, it is changed
somewhere in your dicrionary to. Lists are always copied as pointers,
except explicitly told other wise. So a = b = [] makes a and be the
same list, and a.append(1) makes b - [1].
So do something like mydict[mykey] = mylist[:] (Slicing gives a copy
of the list, not the pointer).
Hope this helps.
Moi
Dolf
Ah -this is exactly what I was doing wrong -thaks very much! Aologies
also for not posting sooner, I have been away for a few days.
Thanks for all of your help,
Ben
On 6 Oc
John Machin wrote:
Steve Holden wrote:
John Machin wrote:
Ben wrote:
>
>>Hello...
>>
>>I have set up a dictionary into whose values I am putting a list. I
>>loop around and around filling my list each time with new values, then
>>dumping this list into the dictionary. Or so I thought...
>>
>>It would appear that what I am dumping into the dictionary value is
>>only a pointer to the original list, so after all my iterations all I
>>have is a dictionary whose every value is equal to that of the list the
>>final time I looped around :-(
>>
>>Is there a way to acheive what I was attempting ? I have done something
>>almost identical with classes in a list before, and in that case a new
>>instance was created for each list entry...
>>
>>
>>I hope this makes some sense, and doesn't seem to head bangingly
>>simple...
>>
>
>
Do you consult your physician over a video link while wearing a ninja
costume down an unlit coal mine at midnight?
>
Please consider the possibility that your description of what you think
your code might be doing is not enough for diagnosis.
>
You may need to supply:
(1) a listing of your code
(2) a small amount of input data
e.g. [(1, 'foo'), (42, 'bar'), (1, 'zot')]
(3) the output you expect from that input:
e.g. {1: ['foo', 'zot'], 42: ['bar']}
>
One of the fascinating things about c.l.py is that sometimes a questin
will be posted that makes almost no sense to me, and somebody else will
casually read the OP's mind, home in on the issue and provide a useful
and relevant answer.
In this case it seems transparent to me, though probably not to you,
that Ben's problem is rootd in the following behaviour, well-known in
python but frequently confusing to noobs:
>>a = [1, 2, 3]
>>firstlist = a
>>a.append('ano ther element')
>>firstlist
[1, 2, 3, 'another element']
>>>
It's quite transparent to me that his symptom is caused by the one list
being used throughout the exercise, instead of one per different dict
key. What you have described is one possibility.
Here's another possibility: Making the charitable assumption that he
has an outer loop and an inner loop, maybe (as I think another poster
has already suggested) all he needs to do is move "mylist = []" inside
the outer loop. Note that he doesn't say explicitly whether the one
list that he gets is the *correct* list for the last key, or whether
it's the catenation of all the correct lists, or something else.
Yet another: Noobs do all sorts of funny things. He could be operating
on a "clean the bucket out after each use instead making a new one"
paradigm:
| >>d= {}
| >>L = []
| >>L.append(1)
| >>L.append(2)
| >>d['a'] = L
| >>d
| {'a': [1, 2]}
| >>del L[:]
| >>d
| {'a': []}
| >>L.append(3)
| >>L.append(4)
| >>d['b'] = L
| >>d
| {'a': [3, 4], 'b': [3, 4]}
Cheers,
John
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Christoph Zwerschke |
last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist
only since Python 2.4.
Anyway, I was thinking about whether it would be possible and desirable
to change the old behavior in future Python versions and let dict.keys()
and dict.values() both return sets instead of lists.
If d is a dict, code like:
for x in d.keys():
|
by: Alexandre Brisebois (www.pointnetsolutions.com) |
last post by:
Hi,
I am currently building a lexical analysis component to pull keywords
out of content,
I currently have a functional first build, but I am having problems
ince I am easily loading over 300 000 strings in memory,
when I am doing the actual analysis I can reach upto 400 mb of ram
usage.
|
by: Nico Grubert |
last post by:
Hi there,
I am looking for a way to sort a list containing dictionaries.
This is my example list:
I want to sort the list by dictionary's key 'from_datetime' so the
sorted list should be:
|
by: vatamane |
last post by:
This has been bothering me for a while. Just want to find out if it
just me or perhaps others have thought of this too: Why shouldn't the
keyset of a dictionary be represented as a set instead of a list? I
know that sets were introduced a lot later and lists/dictionaries were
used instead but I think "the only correct way" now is for the
dictionary keys and values to be sets. Presently {1:0,2:0,3:0}.keys()
will produce but it could also...
|
by: John Henry |
last post by:
Hi list,
I am sure there are many ways of doing comparision but I like to see
what you would do if you have 2 dictionary sets (containing lots of
data - like 20000 keys and each key contains a dozen or so of records)
and you want to build a list of differences about these two sets.
I like to end up with 3 lists: what's in A and not in B, what's in B
and not in A, and of course, what's in both A and B.
| |
by: Ben |
last post by:
Hello...
I have a dictionary, where each value is a seperate instance of the
same class:
self.mop_list=record(self.mops)
In my case I know that record_number takes the values 0,3,and 7 thanks
to a loop, giving me three instances of the record class instantiaterd
with some data I've pased in.
|
by: asincero |
last post by:
Which is better: using an if/else construct to simulate a C switch or
use a dictionary? Example:
def foo():
if c == 1:
doCase1()
elif c == 2:
doCase2()
elif c == 3:
doCase3()
|
by: robinsiebler |
last post by:
I have a data structure that looks like this:
# dates = {'2007': {'25': {'06/23/07': {'aerosmith': ,
# 'Metallica': },
# 'last_song': }}}
I have a section where I try to loop through a set of dates and print
out all of the songs for a given artist:
if type == 'artist':
|
by: Assimalyst |
last post by:
Hi
I have a Dictionary<string, List<string>>, which i have successfully
filled. My problem is I need to create a filter expression using all
possible permutations of its contents.
i.e. the dictionary essentially creates the following array:
Key Value
|
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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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: 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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |