Re: dictionary containing a list

2006-10-09 Thread Hendrik van Rooyen
 Fredrik Lundh [EMAIL PROTECTED] wrote:


 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

LOL - Whining about documentation is what programmers do - its driven by the
fact that the docs and the implementation are never in sync, except on
something that is either trivial, or as old as the hills...

And it does not matter if the software is free and open source, or bought at
great expense - there are always these differences - sometimes niggly, and often
major - it sometimes looks as if the docs were a statement of intent, with the
implementation taking a left turn at the first crossroads.

- Hendrik
- Hendrik

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dictionary containing a list

2006-10-08 Thread Ben
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('another 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dictionary containing a list

2006-10-07 Thread Steve Holden
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('another 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('another 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dictionary containing a list

2006-10-07 Thread Fredrik Lundh
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dictionary containing a list

2006-10-07 Thread John Machin

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('another 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

-- 
http://mail.python.org/mailman/listinfo/python-list


dictionary containing a list

2006-10-06 Thread Ben
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dictionary containing a list

2006-10-06 Thread John Machin

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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dictionary containing a list

2006-10-06 Thread Ben Finney
Ben [EMAIL PROTECTED] writes:

 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dictionary containing a list

2006-10-06 Thread Bryan Olson
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dictionary containing a list

2006-10-06 Thread hanumizzle
On 6 Oct 2006 14:37:59 -0700, Ben [EMAIL PROTECTED] wrote:

 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dictionary containing a list

2006-10-06 Thread goyatlah
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 [EMAIL PROTECTED] 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...


Cheers,

Ben

-- 
http://mail.python.org/mailman/listinfo/python-list