Re: A basic dictionary question

2015-06-11 Thread MRAB
On 2015-06-11 11:10, David Aldrich wrote: Hi I am fairly new to Python. I am writing some code that uses a dictionary to store definitions of hardware registers. Here is a small part of it: import sys register = { 'address' : 0x3001c, 'fields' : { 'FieldA' : {

Re: A basic dictionary question

2015-06-11 Thread Peter Otten
David Aldrich wrote: > Hi > > I am fairly new to Python. I am writing some code that uses a dictionary > to store definitions of hardware registers. Here is a small part of it: > > import sys > > register = { > 'address' : 0x3001c, > 'fields' : { > 'FieldA' : { >

A basic dictionary question

2015-06-11 Thread David Aldrich
Hi I am fairly new to Python. I am writing some code that uses a dictionary to store definitions of hardware registers. Here is a small part of it: import sys register = { 'address' : 0x3001c, 'fields' : { 'FieldA' : { 'range' : (31,20), }, 'FieldB

Re: Basic list/dictionary question

2009-11-11 Thread Mick Krippendorf
Ralax wrote: > On Nov 11, 8:58 pm, Chris Rebert wrote: >> In [2]: def foo(z, a=[]): >>...: a.append(z) >>...: return a >>...: >> >> In [3]: foo(1) >> Out[3]: [1] >> >> In [4]: foo(2) >> Out[4]: [1, 2] >> >> In [5]: foo(2) >> Out[5]: [1, 2, 2] >> >> In [6]: foo(3) >> Out[6]: [1,

Re: Basic list/dictionary question

2009-11-11 Thread Ralax
On Nov 11, 8:58 pm, Chris Rebert wrote: > On Wed, Nov 11, 2009 at 4:16 AM, Daniel Jowett > wrote: > > Greetings, > > > I'm trying to categorize items in a list, by copying them into a > > dictionary... > > A simple example with strings doesn't seem to work how I'd expect: > > basket = ['app

Re: Basic list/dictionary question

2009-11-11 Thread Daniel Jowett
Thanks Chris, yes it's becoming clearer now. And defaultdict looks nice - unfortunately I'm stuck to python 2.4 as I'm using Plone. Thanks again, Daniel 2009/11/11 Chris Rebert > On Wed, Nov 11, 2009 at 4:16 AM, Daniel Jowett > wrote: > > Greetings, > > > > I'm trying to categorize items in

Re: Basic list/dictionary question

2009-11-11 Thread Chris Rebert
On Wed, Nov 11, 2009 at 4:16 AM, Daniel Jowett wrote: > Greetings, > > I'm trying to categorize items in a list, by copying them into a > dictionary... > A simple example with strings doesn't seem to work how I'd expect: > basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']

Basic list/dictionary question

2009-11-11 Thread Daniel Jowett
Greetings, I'm trying to categorize items in a list, by copying them into a dictionary... A simple example with strings doesn't seem to work how I'd expect: >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana'] >>> d = {} >>> d = d.fromkeys(basket, []) >>> d {'orange': [], 'pear':

Re: Dictionary Question

2007-02-09 Thread [EMAIL PROTECTED]
On 9 fév, 04:02, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > En Thu, 08 Feb 2007 23:32:50 -0300, Sick Monkey <[EMAIL PROTECTED]> > escribió: > > > db = {'[EMAIL PROTECTED]':'none', '[EMAIL PROTECTED]':'none', > > '[EMAIL PROTECTED]':'none', > > '[EMAIL PROTECTED]':'none',} > > > And I want to

Re: Dictionary Question

2007-02-08 Thread Gabriel Genellina
At Friday 9/2/2007 00:50, you wrote: Hey Gabriel, Please keep posting on the list - you'll reach a whole lot of people there... Thanks again for the help... but im still having some issues. For some reason the "domsrch.search(key)" is pointing to a memory reference... so when

Re: Dictionary Question

2007-02-08 Thread Gabriel Genellina
En Thu, 08 Feb 2007 23:32:50 -0300, Sick Monkey <[EMAIL PROTECTED]> escribió: > db = {'[EMAIL PROTECTED]':'none', '[EMAIL PROTECTED]':'none', > '[EMAIL PROTECTED]':'none', > '[EMAIL PROTECTED]':'none',} > > And I want to pull out all of the "gmail.com" addresses.. How would I do > this? > > N

Dictionary Question

2007-02-08 Thread Sick Monkey
Hello All. I have a question concerning searching data within dictionaries. Lets say I have a dictionary called db. db = {'[EMAIL PROTECTED]':'none', '[EMAIL PROTECTED]':'none', '[EMAIL PROTECTED]':'none', '[EMAIL PROTECTED]':'none',} And I want to pull out all of the "gmail.com" addresses..

Re: newbie: datastructure `dictionary' question

2006-09-09 Thread Bruno Desthuilliers
jason a écrit : Just some more suggestions: > def parselog(data): > other = 0 > records = {} > > for line in string.split(data, '\n'): for line in data.split('\n'): > str = line.strip() This will shadow the builtin 'str' type. You could reassign to 'line' instead, or

Re: newbie: datastructure `dictionary' question

2006-09-09 Thread jason
On Sat, 09 Sep 2006 09:00:35 -0700, John Machin wrote: > jason wrote: >> Hello, >> >> I am completely new to python and I have question that I unfortunately >> could not find in the various documentation online. My best guess is >> that the answer should be quitte easy but I have just enterd the

Re: newbie: datastructure `dictionary' question

2006-09-09 Thread John Machin
jason wrote: > Hello, > > I am completely new to python and I have question that I unfortunately > could not find in the various documentation online. My best guess is > that the answer should be quitte easy but I have just enterd the learning > phase so that means a hightend chance for stupidity

Re: newbie: datastructure `dictionary' question

2006-09-09 Thread Diez B. Roggisch
jason schrieb: > Hello, > > I am completely new to python and I have question that I unfortunately > could not find in the various documentation online. My best guess is > that the answer should be quitte easy but I have just enterd the learning > phase so that means a hightend chance for stupidit

newbie: datastructure `dictionary' question

2006-09-09 Thread jason
Hello, I am completely new to python and I have question that I unfortunately could not find in the various documentation online. My best guess is that the answer should be quitte easy but I have just enterd the learning phase so that means a hightend chance for stupidity and mistakes on my part.

Re: Dictionary question

2006-07-18 Thread Brian Elmegaard
"Nick Vatamaniuc" <[EMAIL PROTECTED]> writes: > if l[-1].setdefault(a+c, x+e) l[-1][a+c]=x+e Thanks for the answer. I will try it. -- Brian (remove the sport for mail) http://www.et.web.mek.dtu.dk/Staff/be/be.html Rugbyklubben Speed Scandinavian Open 7s Rugby http://www.rkspeed.dk -- http:

Re: Dictionary question

2006-07-18 Thread Brian Elmegaard
"Justin Azoff" <[EMAIL PROTECTED]> writes: > last[keytotal] = min(last.get(keytotal), valtotal) > comes close to working - it would if you were doing max. Thanks, I think this would help. -- Brian (remove the sport for mail) http://www.et.web.mek.dtu.dk/Staff/be/be.html Rugbyklubben Speed Scan

Re: Dictionary question

2006-07-18 Thread Brian Elmegaard
John Machin <[EMAIL PROTECTED]> writes: > 2. Put spaces around operators -- in general, RTFStyleGuide >http://www.python.org/dev/peps/pep-0008 I din't know it. Thanks. > Only you know what *really* meaningful names you should be using. I have better names in my running code. > mykey = a +

Re: Dictionary question

2006-07-18 Thread John Machin
On 18/07/2006 9:51 PM, Brian Elmegaard wrote: > Brian Elmegaard <[EMAIL PROTECTED]> writes: > > At least it was clumsy to post a wrong example. > This shows what = find clumsy. > > c=1 > x=2 > > l=list() > l.append(dict()) > l[0][5]=0 > > l.append(dict()) > > for a, e in l[-2].iteritems(): > #

Re: Dictionary question

2006-07-18 Thread Justin Azoff
Brian Elmegaard wrote: > for a, e in l[-2].iteritems(): > # Can this be written better? > if a+c in l[-1]: > if l[-1][a+c] l[-1][a+c]=x+e > else: > l[-1][a+c]=x+e > # I'd start with something like for a, e in l[-2].iteritems(): keytotal = a

Re: Dictionary question

2006-07-18 Thread Nick Vatamaniuc
Brian, You can try the setdefault method of the dictionary. For a dictionary D the setdefault work like this: D.setdefault(k, defvalue). If k not in D then D[k] is set to defvalue and defvalue is returned. For example: In [1]: d={} In [2]: d.setdefault(1,5) Out[2]:5 In [3]: d Out[3]:{1: 5} In y

Re: Dictionary question

2006-07-18 Thread Brian Elmegaard
Brian Elmegaard <[EMAIL PROTECTED]> writes: At least it was clumsy to post a wrong example. This shows what = find clumsy. c=1 x=2 l=list() l.append(dict()) l[0][5]=0 l.append(dict()) for a, e in l[-2].iteritems(): # Can this be written better? if a+c in l[-1]: if l[-1][a+c

Dictionary question

2006-07-18 Thread Brian Elmegaard
Hi I have written the following which works, but I would like to write it less clumsy. I have a dictionary in which I loop through the keys for a dynamic programming algorithm. If a key is present I test if its value is better than the current, if it is not present I just create it. Would it be p

Re: Tricky Dictionary Question from newbie

2005-07-12 Thread Bengt Richter
On Tue, 12 Jul 2005 11:52:41 -0400, Tim Peters <[EMAIL PROTECTED]> wrote: >[Peter Hansen] >... >> I suppose I shouldn't blame setdefault() itself for being poorly named, > >No, you should blame Guido for that . > >> but it's confusing to me each time I see it in the above, because the >> name does

Re: Tricky Dictionary Question from newbie

2005-07-12 Thread Tim Peters
[Peter Hansen] ... > I suppose I shouldn't blame setdefault() itself for being poorly named, No, you should blame Guido for that . > but it's confusing to me each time I see it in the above, because the > name doesn't emphasize that the value is being returned, and yet that > fact is arguably mor

Re: Tricky Dictionary Question from newbie

2005-07-12 Thread James Carroll
Oops.. Gmail just normally puts the reply to at the bottom of the discussion... so by default I reply to the list, and the last person to post. My comment was not directed at you. I just posted the contents of an interactive session that I did to better understand setdefault myself. I've got to

Re: Tricky Dictionary Question from newbie

2005-07-12 Thread Peter Hansen
(Fixed top-posting) James Carroll wrote: > On 7/11/05, Peter Hansen <[EMAIL PROTECTED]> wrote: >>(I always have to ignore the name to think about how it works, or it >>gets in the way of my understanding it. The name makes fairly little >>sense to me.) > Notice the dictionary is only changed

Re: Tricky Dictionary Question from newbie

2005-07-12 Thread James Carroll
Notice the dictionary is only changed if the key was missing. >>> a = {} >>> a.setdefault("a", "1") '1' >>> a.setdefault("a", "2") '1' >>> a.setdefault("b", "3") '3' >>> a {'a': '1', 'b': '3'} >>> a.setdefault("a", "5") '1' >>> a {'a': '1', 'b': '3'} -Jim On 7/11/05, Peter Hansen <[EMAIL PROTECT

Re: Tricky Dictionary Question from newbie

2005-07-11 Thread Peter Hansen
Ric Da Force wrote: > How does setdefault work exactly? I am looking in the docs and can't figure > it out... If the key (the first argument) already exists in the dictionary, the corresponding value is returned. If the key does not exist in the dictionary, it is stored in the dictionary and b

Re: Tricky Dictionary Question from newbie

2005-07-11 Thread Ric Da Force
How does setdefault work exactly? I am looking in the docs and can't figure it out... Ric "Ric Da Force" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Thank you guys! (Reinhold, Mark and Markus) I must confess that I am > absolutely awe struck at the power of this language! Th

Re: Tricky Dictionary Question from newbie

2005-07-11 Thread Ric Da Force
Thank you guys! (Reinhold, Mark and Markus) I must confess that I am absolutely awe struck at the power of this language! There is no way in the world that I would have envisaged such simple and elegant solutions!!! Reinhold, is your solution specific to 2.4? Kind Regards, Ric "Reinhold Bir

Re: Tricky Dictionary Question from newbie

2005-07-11 Thread Reinhold Birkenfeld
Mark Jackson wrote: > "Ric Da Force" <[EMAIL PROTECTED]> writes: > >> It is hard to explain but this is what I mean: >> >> Dict = {'rt': 'This is repeated', 'sr': 'This is repeated', 'gf': 'This is >> not'} >> >> I want this to return a new dict with string keys and lists containing the >> pre

Re: Tricky Dictionary Question from newbie

2005-07-11 Thread Mark Jackson
"Ric Da Force" <[EMAIL PROTECTED]> writes: > It is hard to explain but this is what I mean: > > Dict = {'rt': 'This is repeated', 'sr': 'This is repeated', 'gf': 'This is > not'} > > I want this to return a new dict with string keys and lists containing the > previous keys for repeated values.

Re: Tricky Dictionary Question from newbie

2005-07-11 Thread Cyril Bazin
Hum... I think an iteritems is better, this way, python don't need to create in memory a complete list of couple key, value.On 7/11/05, Markus Weihs <[EMAIL PROTECTED]> wrote: Hi! Dict = {'rt': 'repeated', 'sr':'repeated', 'gf':'not repeated'} NewDic = {} for k,v in Dict.items(): NewDic.setdef

Re: Tricky Dictionary Question from newbie

2005-07-11 Thread Markus Weihs
Hi! Dict = {'rt': 'repeated', 'sr':'repeated', 'gf':'not repeated'} NewDic = {} for k,v in Dict.items(): NewDic.setdefault(v, []).append(k) Regards, mawe -- http://mail.python.org/mailman/listinfo/python-list

Re: Tricky Dictionary Question from newbie

2005-07-11 Thread Cyril Bazin
Hello, Try that, it may not be the better solution, but it seems to work: #def invertDict(d): #    d2 = {} #    for k, v in d.iteritems(): #    d2.setdefault(v, []).append(k) #    return d2 Cyril On 7/11/05, Ric Da Force <[EMAIL PROTECTED]> wrote: Hi all,I have a dictionary containing about

Tricky Dictionary Question from newbie

2005-07-11 Thread Ric Da Force
Hi all, I have a dictionary containing about 300 items, some of the values being repeated. Both keys and values are strings. How can I turn this thing on its head so that we create a key based on each unique value and build the values based on the keys corresponding to the repeated values? I

Re: Dictionary question.

2005-04-23 Thread bserviss
A simple way to get individual values for the distribution is: d = {} for i in range( 0, 1000): j = random.randrange( 0, 100) if d.has_key(j): d[j] += 1 else: d[j] = 1 keys = d.keys() keys.sort() for key in keys: print key, ":", "*" * d[key] -- http://mail.pytho

Re: Dictionary question.

2005-04-21 Thread Kent Johnson
hawkesed wrote: Actually, I think I got it now. Here is what I did: for num in alist: ... if adict.has_key(num): ... x = adict.get(num) ... x = x + 1 ... adict.update({num:x}) A simpler way to do this last line is adict[num] = x ... else: ...

Re: Dictionary question.

2005-04-21 Thread Brian van den Broek
hawkesed said unto the world upon 2005-04-21 20:28: Actually, I think I got it now. Here is what I did: for num in alist: ... if adict.has_key(num): ... x = adict.get(num) ... x = x + 1 ... adict.update({num:x}) ... else: ... adict.updat

Re: Dictionary question.

2005-04-21 Thread hawkesed
Actually, I think I got it now. Here is what I did: >>> for num in alist: ... if adict.has_key(num): ... x = adict.get(num) ... x = x + 1 ... adict.update({num:x}) ... else: ... adict.update({num:1}) ... >>> adict {128: 2, 129: 2, 132: 1, 15

Re: Dictionary question.

2005-04-21 Thread hawkesed
Steve, thanks for the input. That is actually what I am trying to do, but I don't know the syntax for this in python. For example here is a list I want to work with as input: [101, 66, 75, 107, 108, 101, 106, 98, 111, 88, 119, 93, 115, 95, 114, 95, 118, 109, 85, 75, 88, 97, 53, 78, 98, 91, 115, 77

Re: Dictionary question.

2005-04-21 Thread hawkesed
Here is an example of the input list: [101, 66, 75, 107, 108, 101, 106, 98, 111, 88, 119, 93, 115, 95, 114, 95, 118, 109, 85, 75, 88, 97, 53, 78, 98, 91, 115, 77, 107, 153, 108, 101] Here is the code I am working on now: >>> for num in alist: ... if adict.has_key(num): ... x = adic

Re: Dictionary question.

2005-04-21 Thread Terry Reedy
"hawkesed" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hi, > I am semi new to Python. Here is my problem : I have a list of 100 > random integers. I want to be able to construct a histogram out of the > data. So I want to know how many 70's, 71's, etc. I can't figure out > how

Re: Dictionary question.

2005-04-21 Thread Steve Holden
Simon Brunning wrote: On 21 Apr 2005 02:47:42 -0700, hawkesed <[EMAIL PROTECTED]> wrote: I am semi new to Python. Here is my problem : I have a list of 100 random integers. I want to be able to construct a histogram out of the data. So I want to know how many 70's, 71's, etc. I can't figure out ho

Re: Dictionary question.

2005-04-21 Thread Simon Brunning
On 21 Apr 2005 02:47:42 -0700, hawkesed <[EMAIL PROTECTED]> wrote: > I am semi new to Python. Here is my problem : I have a list of 100 > random integers. I want to be able to construct a histogram out of the > data. So I want to know how many 70's, 71's, etc. I can't figure out > how to do this.

Dictionary question.

2005-04-21 Thread hawkesed
Hi, I am semi new to Python. Here is my problem : I have a list of 100 random integers. I want to be able to construct a histogram out of the data. So I want to know how many 70's, 71's, etc. I can't figure out how to do this. A dictionary is supposedly can do key value pairs right? I want to be

Re: probably weird or stupid newbie dictionary question

2005-02-10 Thread Nick Craig-Wood
Diez B. Roggisch <[EMAIL PROTECTED]> wrote: > But what happens in case of a hash code clash? Then a list of (key, values) > is stored, and for a passed key, each key in that list is additionally > compared for being equal to the passed one. So another requirement of > hashable objecst is the co

Re: probably weird or stupid newbie dictionary question

2005-02-09 Thread hawkmoon269
Very. Thanks much. :-) h -- http://mail.python.org/mailman/listinfo/python-list

Re: probably weird or stupid newbie dictionary question

2005-02-09 Thread hawkmoon269
That makes sense. Thanks. :-) h -- http://mail.python.org/mailman/listinfo/python-list

Re: probably weird or stupid newbie dictionary question

2005-02-09 Thread Stefan Behnel
hawkmoon269 schrieb: some other languages' hash table (Perl's, for instance). But FMU a dictionary's keys are *themselves* hashed so that a hash table exists that maps hashed key values to keys in the dictionary. I guess you're mixing up the terms "hashing" and "storing in a hash-table". When we

Re: probably weird or stupid newbie dictionary question

2005-02-09 Thread Diez B. Roggisch
hawkmoon269 wrote: > I've read in several places that a Python dictionary is analagous to > some other languages' hash table (Perl's, for instance). But FMU a > dictionary's keys are *themselves* hashed so that a hash table exists > that maps hashed key values to keys in the dictionary. ISTM, th

probably weird or stupid newbie dictionary question

2005-02-09 Thread hawkmoon269
I've read in several places that a Python dictionary is analagous to some other languages' hash table (Perl's, for instance). But FMU a dictionary's keys are *themselves* hashed so that a hash table exists that maps hashed key values to keys in the dictionary. ISTM, then, that the analogy is at l