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' : {
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' : {
>
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
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,
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
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
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']
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':
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
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
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
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..
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
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
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
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
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.
"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:
"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
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 +
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():
> #
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
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
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
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
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
[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
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
(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
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
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
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
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
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
"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.
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
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
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
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
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
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:
...
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
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
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
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
"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
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
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.
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
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
Very. Thanks much. :-)
h
--
http://mail.python.org/mailman/listinfo/python-list
That makes sense. Thanks. :-)
h
--
http://mail.python.org/mailman/listinfo/python-list
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
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
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
55 matches
Mail list logo