Re: building a dict

2010-03-14 Thread Steve Holden
Andreas Waldenburger wrote:
 On Sat, 13 Mar 2010 13:42:12 -0800 (PST) vsoler
 vicente.so...@gmail.com wrote:
 
 By the way, I suppose I am the OP. Since I am not an native English
 speaking person, I do not know what it stands for. Perhaps you can
 tell me.

 Perhaps you can find out yourself:
 
 http://www.urbandictionary.com/define.php?term=op
 
Possibly so, and that's a useful site, but I hope you aren't suggesting
that Vicente shouldn't have asked. It seemed like a perfectly acceptable
question to me.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
See PyCon Talks from Atlanta 2010  http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: building a dict

2010-03-14 Thread Andreas Waldenburger
On Sun, 14 Mar 2010 08:36:55 -0400 Steve Holden st...@holdenweb.com
wrote:

 Andreas Waldenburger wrote:
  On Sat, 13 Mar 2010 13:42:12 -0800 (PST) vsoler
  vicente.so...@gmail.com wrote:
  
  By the way, I suppose I am the OP. Since I am not an native English
  speaking person, I do not know what it stands for. Perhaps you can
  tell me.
 
  Perhaps you can find out yourself:
  
  http://www.urbandictionary.com/define.php?term=op
  
 Possibly so, and that's a useful site, but I hope you aren't
 suggesting that Vicente shouldn't have asked. It seemed like a
 perfectly acceptable question to me.
 
I was a bit trigger happy there, I admit. But with vocabulary questions
like these, my first reflex is to ask the web rather than people,
because it's usually quicker than Usenet (albeit often only by a minute
or so). I somehow felt the need to bestow that bit of wisdom on the
world.

But yeah, the question is fine, of course.

/W

-- 
INVALID? DE!

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


building a dict

2010-03-13 Thread vsoler
Say that m is a tuple of 2-tuples

m=(('as',3), ('ab',5), (None, 1), ('as',None), ('as',6))

and I need to build a d dict where each key has an associated list
whose first element is the count, and the second is the sum. If a 2-
tuple contains a None value, it should be discarded.

The expected result is:
d={'as':[2, 9], 'ab': [1,5]}

How should I proceed? So far I have been unsuccessful. I have tried
with a for loop.

Thank you for your help
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: building a dict

2010-03-13 Thread Jon Clements
On 13 Mar, 15:05, vsoler vicente.so...@gmail.com wrote:
 Say that m is a tuple of 2-tuples

 m=(('as',3), ('ab',5), (None, 1), ('as',None), ('as',6))

 and I need to build a d dict where each key has an associated list
 whose first element is the count, and the second is the sum. If a 2-
 tuple contains a None value, it should be discarded.

 The expected result is:
 d={'as':[2, 9], 'ab': [1,5]}

 How should I proceed? So far I have been unsuccessful. I have tried
 with a for loop.

 Thank you for your help

Something like:

d = defaultdict( lambda: [0,0] )
for key, val in filter(lambda L: not any(i is None for i in L), m):
d[key][0] += 1
d[key][1] += val

hth

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


Re: building a dict

2010-03-13 Thread Patrick Maupin
On Mar 13, 9:05 am, vsoler vicente.so...@gmail.com wrote:
 Say that m is a tuple of 2-tuples

 m=(('as',3), ('ab',5), (None, 1), ('as',None), ('as',6))

 and I need to build a d dict where each key has an associated list
 whose first element is the count, and the second is the sum. If a 2-
 tuple contains a None value, it should be discarded.

 The expected result is:
 d={'as':[2, 9], 'ab': [1,5]}

 How should I proceed? So far I have been unsuccessful. I have tried
 with a for loop.

Post your first try at a for loop, and people might be willing to
point out problems, but this is such a basic for loop that it is
unlikely that anybody is going to write your ENTIRE homework for you.

Regards,
Pat
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: building a dict

2010-03-13 Thread Steve Holden
vsoler wrote:
 Say that m is a tuple of 2-tuples
 
 m=(('as',3), ('ab',5), (None, 1), ('as',None), ('as',6))
 
 and I need to build a d dict where each key has an associated list
 whose first element is the count, and the second is the sum. If a 2-
 tuple contains a None value, it should be discarded.
 
 The expected result is:
 d={'as':[2, 9], 'ab': [1,5]}
 
 How should I proceed? So far I have been unsuccessful. I have tried
 with a for loop.
 
 Thank you for your help

Here's a fairly simple-minded approach using a defaultdict, which calls
the dflt() function to create a value when the key is absent.

 from collections import defaultdict
 def dflt():
... return [0, 0]
...
 m = (('as',3), ('ab',5), (None, 1), ('as',None), ('as',6))
 d = defaultdict(dflt)
 for key, n in m:
...   if key is not None and n is not None:
... c, t = d[key]
... d[key] = [c+1, t+n]
...
 d
defaultdict(function dflt at 0x7f0bcb1b0ed8,
{'as': [2, 9], 'ab': [1, 5]})


regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
See PyCon Talks from Atlanta 2010  http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: building a dict

2010-03-13 Thread Jon Clements
On 13 Mar, 15:28, Patrick Maupin pmau...@gmail.com wrote:
 On Mar 13, 9:05 am, vsoler vicente.so...@gmail.com wrote:

  Say that m is a tuple of 2-tuples

  m=(('as',3), ('ab',5), (None, 1), ('as',None), ('as',6))

  and I need to build a d dict where each key has an associated list
  whose first element is the count, and the second is the sum. If a 2-
  tuple contains a None value, it should be discarded.

  The expected result is:
  d={'as':[2, 9], 'ab': [1,5]}

  How should I proceed? So far I have been unsuccessful. I have tried
  with a for loop.

 Post your first try at a for loop, and people might be willing to
 point out problems, but this is such a basic for loop that it is
 unlikely that anybody is going to write your ENTIRE homework for you.

 Regards,
 Pat

I was thinking it's possibly homework, but looking at previous posts
it's fairly unlikely.

(If it is, then mea culpa, but as Steve has replied, I think I'll
manage to sleep tonight not worrying about the influx of uneducated,
incompetent and otherwise useless developers to the market).

However, they're receiving some 'elegant' solutions which no professor
(unless they're a star pupil - in which case they wouldn't be asking)
would take as having been done by their selves.
(Or at least I hope not)

But yes, I would certainly be interested in the 'unsuccessful
attempt'.
(To the OP, do post your attempts, it does give more validity).


Cheers,

Jon.

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


Re: building a dict

2010-03-13 Thread rurpy
On Mar 13, 8:28 am, Patrick Maupin pmau...@gmail.com wrote:
 On Mar 13, 9:05 am, vsoler vicente.so...@gmail.com wrote:

  Say that m is a tuple of 2-tuples

  m=(('as',3), ('ab',5), (None, 1), ('as',None), ('as',6))

  and I need to build a d dict where each key has an associated list
  whose first element is the count, and the second is the sum. If a 2-
  tuple contains a None value, it should be discarded.

  The expected result is:
  d={'as':[2, 9], 'ab': [1,5]}

  How should I proceed? So far I have been unsuccessful. I have tried
  with a for loop.

 Post your first try at a for loop, and people might be willing to
 point out problems, but this is such a basic for loop that it is
 unlikely that anybody is going to write your ENTIRE homework for you.

This is probably what you (OP) were trying to come up with?
[untested]

d = {}
for item in m:
key = m[0];  value = m[1]
if key is None or value is None: continue
if key not in dict:
d[key] = [value]
else:
d[key].append (value)

You can replace the
  for item in m:
  key = m[0];  value = m[1]
above with
  for key, value in m:
which is a little nicer.

However, as other responses point out, when you want
to accumulate results in a dict, collections.defaultdict
should pop into your mind first.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: building a dict

2010-03-13 Thread rurpy
On Mar 13, 9:13 am, ru...@yahoo.com wrote:
 On Mar 13, 8:28 am, Patrick Maupin pmau...@gmail.com wrote:



  On Mar 13, 9:05 am, vsoler vicente.so...@gmail.com wrote:

   Say that m is a tuple of 2-tuples

   m=(('as',3), ('ab',5), (None, 1), ('as',None), ('as',6))

   and I need to build a d dict where each key has an associated list
   whose first element is the count, and the second is the sum. If a 2-
   tuple contains a None value, it should be discarded.

   The expected result is:
   d={'as':[2, 9], 'ab': [1,5]}

   How should I proceed? So far I have been unsuccessful. I have tried
   with a for loop.

  Post your first try at a for loop, and people might be willing to
  point out problems, but this is such a basic for loop that it is
  unlikely that anybody is going to write your ENTIRE homework for you.

 This is probably what you (OP) were trying to come up with?
 [untested]

 d = {}
 for item in m:
     key = m[0];  value = m[1]
     if key is None or value is None: continue
     if key not in dict:
         d[key] = [value]
     else:
         d[key].append (value)

 You can replace the
   for item in m:
       key = m[0];  value = m[1]
 above with
   for key, value in m:
 which is a little nicer.

 However, as other responses point out, when you want
 to accumulate results in a dict, collections.defaultdict
 should pop into your mind first.

Oops, didn't read very carefully, did I?

That should be:
d = {}
for item in m:
key = m[0];  value = m[1]
if key is None or value is None: continue
if key not in dict:
d[key] = [1, value]
else:
d[key][0] += 1
d[key][1] += value

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


Re: building a dict

2010-03-13 Thread rurpy
On Mar 13, 9:26 am, ru...@yahoo.com wrote:
 That should be:
 d = {}
 for item in m:
      key = item[0];  value = item[1]
     if key is None or value is None: continue
     if key not in dict:
         d[key] = [1, value]
     else:
         d[key][0] += 1
         d[key][1] += value

That's it.  Any other mistakes, you find 'em.

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


Re: building a dict

2010-03-13 Thread vsoler
On 13 mar, 18:16, ru...@yahoo.com wrote:
 On Mar 13, 9:26 am, ru...@yahoo.com wrote: That should be:
  d = {}
  for item in m:

       key = item[0];  value = item[1]

      if key is None or value is None: continue
      if key not in dict:
          d[key] = [1, value]
      else:
          d[key][0] += 1
          d[key][1] += value

 That's it.  Any other mistakes, you find 'em.

Thank you all. Your answers are more than valuable to me. I'll study
them carefully, but no doubt, my post has been answered.

By the way, I suppose I am the OP. Since I am not an native English
speaking person, I do not know what it stands for. Perhaps you can
tell me.

From what I see from your posts, you would have preferred that I
included in my original post my for loop, so that the post is not so
abstract. I have taken note and I'll make it better next time.

Thank you for your help.

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


Re: building a dict

2010-03-13 Thread rurpy
On Mar 13, 2:42 pm, vsoler vicente.so...@gmail.com wrote:

 By the way, I suppose I am the OP. Since I am not an native English
 speaking person, I do not know what it stands for. Perhaps you can
 tell me.

OP means Original Poster (the person who started the discussion)
or sometimes Original Post, depending on context.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: building a dict

2010-03-13 Thread Andreas Waldenburger
On Sat, 13 Mar 2010 13:42:12 -0800 (PST) vsoler
vicente.so...@gmail.com wrote:

 By the way, I suppose I am the OP. Since I am not an native English
 speaking person, I do not know what it stands for. Perhaps you can
 tell me.
 
Perhaps you can find out yourself:

http://www.urbandictionary.com/define.php?term=op

/W


-- 
INVALID? DE!

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


Efficiently building ordered dict

2010-02-22 Thread Bryan
I am looping through a list and creating a regular dictionary.  From
that dict, I create an ordered dict.  I can't think of a way to build
the ordered dict while going through the original loop.  Is there a
way I can avoid creating the first unordered dict just to get the
ordered dict?  Also, I am using pop(k) to retrieve the values from the
unordered dict while building the ordered one because I figure that as
the values are removed from the unordered dict, the lookups will
become faster.  Is there a better idiom that the code below to create
an ordered dict from an unordered list?

unorderedDict = {}
for thing in unorderedList:
if thing.id in unorderedDict:
UpdateExistingValue(unorderedDict[thing.id])
else:
CreateNewValue(unorderedDict[thing.id])

orderedDict = OrderedDict()
for k in sorted(unorderedDict.keys()):
orderedDict[k]  unorderedDict.pop(k)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Efficiently building ordered dict

2010-02-22 Thread Shashwat Anand
OrderedDict is a class in collection module in python 2.7a3+. Perhaps you
can use it from there.

 dir(collections)
['Callable', 'Container', 'Counter', 'Hashable', 'ItemsView', 'Iterable',
'Iterator', 'KeysView', 'Mapping', 'MappingView', 'MutableMapping',
'MutableSequence', 'MutableSet', 'OrderedDict', 'Sequence', 'Set', 'Sized',
'ValuesView', '_Link', '__all__', '__builtins__', '__doc__', '__file__',
'__name__', '__package__', '_abcoll', '_chain', '_eq', '_heapq', '_ifilter',
'_imap', '_iskeyword', '_itemgetter', '_proxy', '_repeat', '_starmap',
'_sys', 'defaultdict', 'deque', 'namedtuple']

~l0nwlf

On Mon, Feb 22, 2010 at 10:02 PM, Bryan bryanv...@gmail.com wrote:

 I am looping through a list and creating a regular dictionary.  From
 that dict, I create an ordered dict.  I can't think of a way to build
 the ordered dict while going through the original loop.  Is there a
 way I can avoid creating the first unordered dict just to get the
 ordered dict?  Also, I am using pop(k) to retrieve the values from the
 unordered dict while building the ordered one because I figure that as
 the values are removed from the unordered dict, the lookups will
 become faster.  Is there a better idiom that the code below to create
 an ordered dict from an unordered list?

 unorderedDict = {}
 for thing in unorderedList:
if thing.id in unorderedDict:
UpdateExistingValue(unorderedDict[thing.id])
else:
CreateNewValue(unorderedDict[thing.id])

 orderedDict = OrderedDict()
 for k in sorted(unorderedDict.keys()):
orderedDict[k]  unorderedDict.pop(k)
 --
 http://mail.python.org/mailman/listinfo/python-list

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


Re: Efficiently building ordered dict

2010-02-22 Thread Daniel Stutzbach
On Mon, Feb 22, 2010 at 10:32 AM, Bryan bryanv...@gmail.com wrote:

 unorderedDict = {}
 for thing in unorderedList:
if thing.id in unorderedDict:
UpdateExistingValue(unorderedDict[thing.id])
else:
CreateNewValue(unorderedDict[thing.id])

 orderedDict = OrderedDict()
 for k in sorted(unorderedDict.keys()):
orderedDict[k]  unorderedDict.pop(k)


It's not entirely clear what UpdateExistingValue and CreateNewValue do.
However, it looks like you are trying to create a dictionary where the keys
are sorted.  Is that right?

If so, you could use the sorteddict type from my blist package, which is
similar to an OrderDict except it efficiently keeps the keys in sorted order
instead of insertion order.  For example:

 from blist import sorteddict
 my_dict = sorteddict({1: 'a', 6: 'b', -5: 'c'})
 my_dict.keys()
[-5, 1, 6]
 my_dict[2] = 'd'
 my_dict.keys()
[-5, 1, 2, 6]

It's available here:
http://pypi.python.org/pypi/blist/
--
Daniel Stutzbach, Ph.D.
President, Stutzbach Enterprises, LLC http://stutzbachenterprises.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Efficiently building ordered dict

2010-02-22 Thread MRAB

Bryan wrote:

I am looping through a list and creating a regular dictionary.  From
that dict, I create an ordered dict.  I can't think of a way to build
the ordered dict while going through the original loop.  Is there a
way I can avoid creating the first unordered dict just to get the
ordered dict?  Also, I am using pop(k) to retrieve the values from the
unordered dict while building the ordered one because I figure that as
the values are removed from the unordered dict, the lookups will
become faster.  Is there a better idiom that the code below to create
an ordered dict from an unordered list?


Why are you building a dict from a list and then an ordered dict from
that? Just build the ordered dict from the list, because it's behaves
like a dict, except for remembering the order in which the keys were
added.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Efficiently building ordered dict

2010-02-22 Thread Bryan
On Feb 22, 9:19 am, MRAB pyt...@mrabarnett.plus.com wrote:
 Bryan wrote:
  I am looping through a list and creating a regular dictionary.  From
  that dict, I create an ordered dict.  I can't think of a way to build
  the ordered dict while going through the original loop.  Is there a
  way I can avoid creating the first unordered dict just to get the
  ordered dict?  Also, I am using pop(k) to retrieve the values from the
  unordered dict while building the ordered one because I figure that as
  the values are removed from the unordered dict, the lookups will
  become faster.  Is there a better idiom that the code below to create
  an ordered dict from an unordered list?

 Why are you building a dict from a list and then an ordered dict from
 that? Just build the ordered dict from the list, because it's behaves
 like a dict, except for remembering the order in which the keys were
 added.

Could you write some pseudo-code for that?  I'm not sure how I would
add the items to the OrderedDict while looping through the list.
Wouldn't the list need to be sorted first (which in this case isn't
practical)?



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


Re: Efficiently building ordered dict

2010-02-22 Thread MRAB

Bryan wrote:

On Feb 22, 9:19 am, MRAB pyt...@mrabarnett.plus.com wrote:

Bryan wrote:

I am looping through a list and creating a regular dictionary.  From
that dict, I create an ordered dict.  I can't think of a way to build
the ordered dict while going through the original loop.  Is there a
way I can avoid creating the first unordered dict just to get the
ordered dict?  Also, I am using pop(k) to retrieve the values from the
unordered dict while building the ordered one because I figure that as
the values are removed from the unordered dict, the lookups will
become faster.  Is there a better idiom that the code below to create
an ordered dict from an unordered list?

Why are you building a dict from a list and then an ordered dict from
that? Just build the ordered dict from the list, because it's behaves
like a dict, except for remembering the order in which the keys were
added.


Could you write some pseudo-code for that?  I'm not sure how I would
add the items to the OrderedDict while looping through the list.
Wouldn't the list need to be sorted first (which in this case isn't
practical)?


ordered != sorted.

If you want the ordered dict to be sorted by key then build a dict first
and then create the ordered dict from the sorted dict. I think the
quickest way to build the sorted dict is:

orderedDict = OrderedDict(sorted(unorderedDict.items()))

although I haven't tried 'sorteddict' (see Daniel Stutzbach's post).
--
http://mail.python.org/mailman/listinfo/python-list


Re: Efficiently building ordered dict

2010-02-22 Thread Arnaud Delobelle
Bryan bryanv...@gmail.com writes:

 I am looping through a list and creating a regular dictionary.  From
 that dict, I create an ordered dict.  I can't think of a way to build
 the ordered dict while going through the original loop.  Is there a
 way I can avoid creating the first unordered dict just to get the
 ordered dict?  Also, I am using pop(k) to retrieve the values from the
 unordered dict while building the ordered one because I figure that as
 the values are removed from the unordered dict, the lookups will
 become faster.  Is there a better idiom that the code below to create
 an ordered dict from an unordered list?

 unorderedDict = {}
 for thing in unorderedList:
   if thing.id in unorderedDict:
   UpdateExistingValue(unorderedDict[thing.id])
   else:
   CreateNewValue(unorderedDict[thing.id])

 orderedDict = OrderedDict()
 for k in sorted(unorderedDict.keys()):
   orderedDict[k]  unorderedDict.pop(k)

Why don't you sort your unorderedList first then simply create your
orderedDict?

To me your problem is stated in too vague terms anyway and the code you
post could not really work as unorderedDict is bound to remain empty
whatever the values of all the other names.

At any rate, why do you need an ordered dictionary?  It seems suspect to
me as you sort the keys before inserting the items.

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


Re: Efficiently building ordered dict

2010-02-22 Thread Mark Lawrence

Bryan wrote:

On Feb 22, 9:19 am, MRAB pyt...@mrabarnett.plus.com wrote:

Bryan wrote:

I am looping through a list and creating a regular dictionary.  From
that dict, I create an ordered dict.  I can't think of a way to build
the ordered dict while going through the original loop.  Is there a
way I can avoid creating the first unordered dict just to get the
ordered dict?  Also, I am using pop(k) to retrieve the values from the
unordered dict while building the ordered one because I figure that as
the values are removed from the unordered dict, the lookups will
become faster.  Is there a better idiom that the code below to create
an ordered dict from an unordered list?

Why are you building a dict from a list and then an ordered dict from
that? Just build the ordered dict from the list, because it's behaves
like a dict, except for remembering the order in which the keys were
added.


Could you write some pseudo-code for that?  I'm not sure how I would
add the items to the OrderedDict while looping through the list.
Wouldn't the list need to be sorted first (which in this case isn't
practical)?





Could you please clarify what you are trying to achieve as you appear to 
be confusing ordering with sorting.


Mark Lawrence.

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


Re: Efficiently building ordered dict

2010-02-22 Thread Alf P. Steinbach

* Bryan:

I am looping through a list and creating a regular dictionary.  From
that dict, I create an ordered dict.  I can't think of a way to build
the ordered dict while going through the original loop.  Is there a
way I can avoid creating the first unordered dict just to get the
ordered dict?  Also, I am using pop(k) to retrieve the values from the
unordered dict while building the ordered one because I figure that as
the values are removed from the unordered dict, the lookups will
become faster.  Is there a better idiom that the code below to create
an ordered dict from an unordered list?

unorderedDict = {}
for thing in unorderedList:
if thing.id in unorderedDict:
UpdateExistingValue(unorderedDict[thing.id])
else:
CreateNewValue(unorderedDict[thing.id])


If this were real code the last statement would generate an exception.



orderedDict = OrderedDict()
for k in sorted(unorderedDict.keys()):
orderedDict[k]  unorderedDict.pop(k)


This is not even valid syntax.


Please

  (1) explain the problem that you're trying to solve, not how you
  imagine the solution, and

  (2) if you have any code, please post real code (copy and paste).

The above code is not real.


Cheers  hth.,

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


Re: Efficiently building ordered dict

2010-02-22 Thread Bryan
On Feb 22, 10:57 am, Alf P. Steinbach al...@start.no wrote:
 * Bryan:



  I am looping through a list and creating a regular dictionary.  From
  that dict, I create an ordered dict.  I can't think of a way to build
  the ordered dict while going through the original loop.  Is there a
  way I can avoid creating the first unordered dict just to get the
  ordered dict?  Also, I am using pop(k) to retrieve the values from the
  unordered dict while building the ordered one because I figure that as
  the values are removed from the unordered dict, the lookups will
  become faster.  Is there a better idiom that the code below to create
  an ordered dict from an unordered list?

  unorderedDict = {}
  for thing in unorderedList:
     if thing.id in unorderedDict:
             UpdateExistingValue(unorderedDict[thing.id])
     else:
             CreateNewValue(unorderedDict[thing.id])

 If this were real code the last statement would generate an exception.

  orderedDict = OrderedDict()
  for k in sorted(unorderedDict.keys()):
     orderedDict[k]  unorderedDict.pop(k)

 This is not even valid syntax.

 Please

    (1) explain the problem that you're trying to solve, not how you
        imagine the solution, and

    (2) if you have any code, please post real code (copy and paste).

 The above code is not real.

 Cheers  hth.,

 - Alf

Sorry about the sorted != ordered mix up.  I want to end up with a
*sorted* dict from an unordered list.  *Sorting the list is not
practical in this case.*  I am using python 2.5, with an ActiveState
recipe for an OrderedDict.

Given these requirements/limitations, how would you do it?

My solution is to create a regular dict from the list.  Then sort the
keys, and add the keys+values to an OrderedDict.  Since the keys are
being added to the OrderedDict in the correctly sorted order, at the
end I end up with a OrderedDict that is in the correctly *sorted*
order.

self.accTotals = {}
for row in self.rows:
if row.acc.code in self.accTotals:
self.accTotals[row.acc.code].addRow(row)
else:
accSummary = Total()
accSummary.addRow(row)
self.accTotals[row.acc.code] = accSummary
self.accTotals = self._getOrderedDict(self.accTotals)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Efficiently building ordered dict

2010-02-22 Thread Arnaud Delobelle
On 22 Feb, 21:29, Bryan bryanv...@gmail.com wrote:
 Sorry about the sorted != ordered mix up.  I want to end up with a
 *sorted* dict from an unordered list.  *Sorting the list is not
 practical in this case.*  I am using python 2.5, with an ActiveState
 recipe for an OrderedDict.

Why does the dict need to be sorted?
Why is it impractical to sort the list, but practical to sort the
dict?

Whithout knowing this, it is difficult to get an idea of your problem
an a potential solution.

 My solution is to create a regular dict from the list.  Then sort the
 keys, and add the keys+values to an OrderedDict.  Since the keys are
 being added to the OrderedDict in the correctly sorted order, at the
 end I end up with a OrderedDict that is in the correctly *sorted*
 order.

 self.accTotals = {}
 for row in self.rows:
         if row.acc.code in self.accTotals:
                 self.accTotals[row.acc.code].addRow(row)
         else:
                 accSummary = Total()
                 accSummary.addRow(row)
                 self.accTotals[row.acc.code] = accSummary
 self.accTotals = self._getOrderedDict(self.accTotals)

This code is a typical example where defaultdict, which was added in
Python 2.5 [1],  would be of use:

accTotals = defaultdict(Total)
for row in self.rows:
accTotals[row.acc.code].addRow(row)
self.accTotals = self._getOrderedDict(accTotals)

However, as you don't explain what self._getOrderedDict(...) does, it
is quite difficult to guess how to improve it!

[1] http://docs.python.org/library/collections.html#collections.defaultdict
--
Arnaud
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Efficiently building ordered dict

2010-02-22 Thread Diez B. Roggisch

Am 22.02.10 22:29, schrieb Bryan:

On Feb 22, 10:57 am, Alf P. Steinbachal...@start.no  wrote:

* Bryan:




I am looping through a list and creating a regular dictionary.  From
that dict, I create an ordered dict.  I can't think of a way to build
the ordered dict while going through the original loop.  Is there a
way I can avoid creating the first unordered dict just to get the
ordered dict?  Also, I am using pop(k) to retrieve the values from the
unordered dict while building the ordered one because I figure that as
the values are removed from the unordered dict, the lookups will
become faster.  Is there a better idiom that the code below to create
an ordered dict from an unordered list?



unorderedDict = {}
for thing in unorderedList:
if thing.id in unorderedDict:
UpdateExistingValue(unorderedDict[thing.id])
else:
CreateNewValue(unorderedDict[thing.id])


If this were real code the last statement would generate an exception.


orderedDict = OrderedDict()
for k in sorted(unorderedDict.keys()):
orderedDict[k]  unorderedDict.pop(k)


This is not even valid syntax.

Please

(1) explain the problem that you're trying to solve, not how you
imagine the solution, and

(2) if you have any code, please post real code (copy and paste).

The above code is not real.

Cheers  hth.,

- Alf


Sorry about the sorted != ordered mix up.  I want to end up with a
*sorted* dict from an unordered list.  *Sorting the list is not
practical in this case.*  I am using python 2.5, with an ActiveState
recipe for an OrderedDict.

Given these requirements/limitations, how would you do it?

My solution is to create a regular dict from the list.  Then sort the
keys, and add the keys+values to an OrderedDict.  Since the keys are
being added to the OrderedDict in the correctly sorted order, at the
end I end up with a OrderedDict that is in the correctly *sorted*
order.


If that works for you, I don't understand your assertion that you can't 
sort the list. If you have the space  time to sort the intermediate 
dict, then it's as easy to create the list  sort  then the ordered 
dict from it. It should be faster, because you sort the keys anyway.


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


Re: Efficiently building ordered dict

2010-02-22 Thread Bryan
On Feb 22, 2:16 pm, Diez B. Roggisch de...@nospam.web.de wrote:
 Am 22.02.10 22:29, schrieb Bryan:



  On Feb 22, 10:57 am, Alf P. Steinbachal...@start.no  wrote:
  * Bryan:

  I am looping through a list and creating a regular dictionary.  From
  that dict, I create an ordered dict.  I can't think of a way to build
  the ordered dict while going through the original loop.  Is there a
  way I can avoid creating the first unordered dict just to get the
  ordered dict?  Also, I am using pop(k) to retrieve the values from the
  unordered dict while building the ordered one because I figure that as
  the values are removed from the unordered dict, the lookups will
  become faster.  Is there a better idiom that the code below to create
  an ordered dict from an unordered list?

  unorderedDict = {}
  for thing in unorderedList:
      if thing.id in unorderedDict:
              UpdateExistingValue(unorderedDict[thing.id])
      else:
              CreateNewValue(unorderedDict[thing.id])

  If this were real code the last statement would generate an exception.

  orderedDict = OrderedDict()
  for k in sorted(unorderedDict.keys()):
      orderedDict[k]  unorderedDict.pop(k)

  This is not even valid syntax.

  Please

      (1) explain the problem that you're trying to solve, not how you
          imagine the solution, and

      (2) if you have any code, please post real code (copy and paste).

  The above code is not real.

  Cheers  hth.,

  - Alf

  Sorry about the sorted != ordered mix up.  I want to end up with a
  *sorted* dict from an unordered list.  *Sorting the list is not
  practical in this case.*  I am using python 2.5, with an ActiveState
  recipe for an OrderedDict.

  Given these requirements/limitations, how would you do it?

  My solution is to create a regular dict from the list.  Then sort the
  keys, and add the keys+values to an OrderedDict.  Since the keys are
  being added to the OrderedDict in the correctly sorted order, at the
  end I end up with a OrderedDict that is in the correctly *sorted*
  order.

 If that works for you, I don't understand your assertion that you can't
 sort the list. If you have the space  time to sort the intermediate
 dict, then it's as easy to create the list  sort  then the ordered
 dict from it. It should be faster, because you sort the keys anyway.

 Diez

Here is how I am converting a regular dict to an ordered dict that is
sorted by keys.

def _getOrderedDict(theDict):
ordered = OrderedDict()
for k in sorted(theDict.keys()):
ordered[k] = theDict.pop(k)
return ordered


The list is a bunch of objects that represent hours worked by
employees on particular jobs, and accounts, and client purchase orders
etc.  From this list, I am generating these sorted dicts that contain
summarizing information about the list.  So one of the sorted dicts
will give a summary of hours worked by job number.  Another one will
give summary information by client PO number etc.  So instead of
sorting the list a bunch of different ways, I keep the list as is,
generate the summaries I need into dictionaries, and then sort those
dictionaries as appropriate.




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


Re: Efficiently building ordered dict

2010-02-22 Thread Diez B. Roggisch

Am 22.02.10 23:48, schrieb Bryan:

On Feb 22, 2:16 pm, Diez B. Roggischde...@nospam.web.de  wrote:

Am 22.02.10 22:29, schrieb Bryan:




On Feb 22, 10:57 am, Alf P. Steinbachal...@start.nowrote:

* Bryan:



I am looping through a list and creating a regular dictionary.  From
that dict, I create an ordered dict.  I can't think of a way to build
the ordered dict while going through the original loop.  Is there a
way I can avoid creating the first unordered dict just to get the
ordered dict?  Also, I am using pop(k) to retrieve the values from the
unordered dict while building the ordered one because I figure that as
the values are removed from the unordered dict, the lookups will
become faster.  Is there a better idiom that the code below to create
an ordered dict from an unordered list?



unorderedDict = {}
for thing in unorderedList:
 if thing.id in unorderedDict:
 UpdateExistingValue(unorderedDict[thing.id])
 else:
 CreateNewValue(unorderedDict[thing.id])



If this were real code the last statement would generate an exception.



orderedDict = OrderedDict()
for k in sorted(unorderedDict.keys()):
 orderedDict[k]  unorderedDict.pop(k)



This is not even valid syntax.



Please



 (1) explain the problem that you're trying to solve, not how you
 imagine the solution, and



 (2) if you have any code, please post real code (copy and paste).



The above code is not real.



Cheershth.,



- Alf



Sorry about the sorted != ordered mix up.  I want to end up with a
*sorted* dict from an unordered list.  *Sorting the list is not
practical in this case.*  I am using python 2.5, with an ActiveState
recipe for an OrderedDict.



Given these requirements/limitations, how would you do it?



My solution is to create a regular dict from the list.  Then sort the
keys, and add the keys+values to an OrderedDict.  Since the keys are
being added to the OrderedDict in the correctly sorted order, at the
end I end up with a OrderedDict that is in the correctly *sorted*
order.


If that works for you, I don't understand your assertion that you can't
sort the list. If you have the space  time to sort the intermediate
dict, then it's as easy to create the list  sort  then the ordered
dict from it. It should be faster, because you sort the keys anyway.

Diez


Here is how I am converting a regular dict to an ordered dict that is
sorted by keys.

def _getOrderedDict(theDict):
ordered = OrderedDict()
for k in sorted(theDict.keys()):
ordered[k] = theDict.pop(k)
return ordered


The list is a bunch of objects that represent hours worked by
employees on particular jobs, and accounts, and client purchase orders
etc.  From this list, I am generating these sorted dicts that contain
summarizing information about the list.  So one of the sorted dicts
will give a summary of hours worked by job number.  Another one will
give summary information by client PO number etc.  So instead of
sorting the list a bunch of different ways, I keep the list as is,
generate the summaries I need into dictionaries, and then sort those
dictionaries as appropriate.


Again - why? Unless there is some filtering going on that reduces the 
number of total entries before the sorting when building the 
intermediate dict, a simple


ordered = OrderedDict(sorted(the_list, key=lambda v: v['some_key']))

won't cost you a dime more.

I think you believe in building the dict so that ou can have the key for 
sorting. As shown abov - you don't need to.


It might even benefitial to really re-sort the original list, because 
that spares you the intermediate list.



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


Re: Efficiently building ordered dict

2010-02-22 Thread MRAB

Bryan wrote:

On Feb 22, 2:16 pm, Diez B. Roggisch de...@nospam.web.de wrote:

Am 22.02.10 22:29, schrieb Bryan:




On Feb 22, 10:57 am, Alf P. Steinbachal...@start.no  wrote:

* Bryan:

I am looping through a list and creating a regular dictionary.  From
that dict, I create an ordered dict.  I can't think of a way to build
the ordered dict while going through the original loop.  Is there a
way I can avoid creating the first unordered dict just to get the
ordered dict?  Also, I am using pop(k) to retrieve the values from the
unordered dict while building the ordered one because I figure that as
the values are removed from the unordered dict, the lookups will
become faster.  Is there a better idiom that the code below to create
an ordered dict from an unordered list?
unorderedDict = {}
for thing in unorderedList:
if thing.id in unorderedDict:
UpdateExistingValue(unorderedDict[thing.id])
else:
CreateNewValue(unorderedDict[thing.id])

If this were real code the last statement would generate an exception.

orderedDict = OrderedDict()
for k in sorted(unorderedDict.keys()):
orderedDict[k]  unorderedDict.pop(k)

This is not even valid syntax.
Please
(1) explain the problem that you're trying to solve, not how you
imagine the solution, and
(2) if you have any code, please post real code (copy and paste).
The above code is not real.
Cheers  hth.,
- Alf

Sorry about the sorted != ordered mix up.  I want to end up with a
*sorted* dict from an unordered list.  *Sorting the list is not
practical in this case.*  I am using python 2.5, with an ActiveState
recipe for an OrderedDict.
Given these requirements/limitations, how would you do it?
My solution is to create a regular dict from the list.  Then sort the
keys, and add the keys+values to an OrderedDict.  Since the keys are
being added to the OrderedDict in the correctly sorted order, at the
end I end up with a OrderedDict that is in the correctly *sorted*
order.

If that works for you, I don't understand your assertion that you can't
sort the list. If you have the space  time to sort the intermediate
dict, then it's as easy to create the list  sort  then the ordered
dict from it. It should be faster, because you sort the keys anyway.

Diez


Here is how I am converting a regular dict to an ordered dict that is
sorted by keys.

def _getOrderedDict(theDict):
ordered = OrderedDict()
for k in sorted(theDict.keys()):
ordered[k] = theDict.pop(k)
return ordered


As I mentioned in an earlier post, you could do:

def _getOrderedDict(theDict):
return OrderedDict(sorted(theDict.items()))

[snip]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Efficiently building ordered dict

2010-02-22 Thread Bryan
On Feb 22, 3:00 pm, Diez B. Roggisch de...@nospam.web.de wrote:
 Am 22.02.10 23:48, schrieb Bryan:



  On Feb 22, 2:16 pm, Diez B. Roggischde...@nospam.web.de  wrote:
  Am 22.02.10 22:29, schrieb Bryan:

  On Feb 22, 10:57 am, Alf P. Steinbachal...@start.no    wrote:
  * Bryan:

  I am looping through a list and creating a regular dictionary.  From
  that dict, I create an ordered dict.  I can't think of a way to build
  the ordered dict while going through the original loop.  Is there a
  way I can avoid creating the first unordered dict just to get the
  ordered dict?  Also, I am using pop(k) to retrieve the values from the
  unordered dict while building the ordered one because I figure that as
  the values are removed from the unordered dict, the lookups will
  become faster.  Is there a better idiom that the code below to create
  an ordered dict from an unordered list?

  unorderedDict = {}
  for thing in unorderedList:
       if thing.id in unorderedDict:
               UpdateExistingValue(unorderedDict[thing.id])
       else:
               CreateNewValue(unorderedDict[thing.id])

  If this were real code the last statement would generate an exception.

  orderedDict = OrderedDict()
  for k in sorted(unorderedDict.keys()):
       orderedDict[k]  unorderedDict.pop(k)

  This is not even valid syntax.

  Please

       (1) explain the problem that you're trying to solve, not how you
           imagine the solution, and

       (2) if you have any code, please post real code (copy and paste).

  The above code is not real.

  Cheers    hth.,

  - Alf

  Sorry about the sorted != ordered mix up.  I want to end up with a
  *sorted* dict from an unordered list.  *Sorting the list is not
  practical in this case.*  I am using python 2.5, with an ActiveState
  recipe for an OrderedDict.

  Given these requirements/limitations, how would you do it?

  My solution is to create a regular dict from the list.  Then sort the
  keys, and add the keys+values to an OrderedDict.  Since the keys are
  being added to the OrderedDict in the correctly sorted order, at the
  end I end up with a OrderedDict that is in the correctly *sorted*
  order.

  If that works for you, I don't understand your assertion that you can't
  sort the list. If you have the space  time to sort the intermediate
  dict, then it's as easy to create the list  sort  then the ordered
  dict from it. It should be faster, because you sort the keys anyway.

  Diez

  Here is how I am converting a regular dict to an ordered dict that is
  sorted by keys.

  def _getOrderedDict(theDict):
     ordered = OrderedDict()
     for k in sorted(theDict.keys()):
             ordered[k] = theDict.pop(k)
     return ordered

  The list is a bunch of objects that represent hours worked by
  employees on particular jobs, and accounts, and client purchase orders
  etc.  From this list, I am generating these sorted dicts that contain
  summarizing information about the list.  So one of the sorted dicts
  will give a summary of hours worked by job number.  Another one will
  give summary information by client PO number etc.  So instead of
  sorting the list a bunch of different ways, I keep the list as is,
  generate the summaries I need into dictionaries, and then sort those
  dictionaries as appropriate.

 Again - why? Unless there is some filtering going on that reduces the
 number of total entries before the sorting when building the
 intermediate dict, a simple

 ordered = OrderedDict(sorted(the_list, key=lambda v: v['some_key']))

 won't cost you a dime more.

 I think you believe in building the dict so that ou can have the key for
 sorting. As shown abov - you don't need to.

 It might even benefitial to really re-sort the original list, because
 that spares you the intermediate list.

 Diez

Lets say my list has 1 million items.  If I sort the list before
summarizing it, I will absolutley have to sort 1 million items.

However, if I summarize first, then just sort the keys in the summary
dict, I could wind up only sorting 4 or 5 items, if within that 1
million item list, there were only 4 or 5 job numbers for example.
The list stays as is, and my summary dictionary is sorted so I can
iterate through it and make little reports that humans will like.


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


Re: Efficiently building ordered dict

2010-02-22 Thread John Posner

On 2/22/2010 4:29 PM, Bryan wrote:


Sorry about the sorted != ordered mix up.  I want to end up with a
*sorted* dict from an unordered list.  *Sorting the list is not
practical in this case.*  I am using python 2.5, with an ActiveState
recipe for an OrderedDict.



Have you looked at this:

  http://pypi.python.org/pypi/sorteddict/1.2.1

 data = zip('afcedbijhg', range(10))
 old = dict(data)
 for key in old:
... print key, old[key]
...
a 0
c 2
b 5
e 3
d 4
g 9
f 1
i 6
h 8
j 7
 new = sorteddict.sorteddict(data)
 for key in new:
... print key, new[key]
...
a 0
b 5
c 2
d 4
e 3
f 1
g 9
h 8
i 6
j 7

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


Building a dict from a tuple of tuples

2010-02-20 Thread vsoler
Hello everyone!

I have a tuple of tuples, coming from an Excel range, such as this:

((None, u'x', u'y'),
(u'a', 1.0, 7.0),
(u'b', None, 8.0))

I need to build a dictionary that has, as key, the row and column
header.

For example:
d={ (u'a',u'x'):1.0, (u'a',u'y'): 7.0, (u'b',u'y'):8.0 }

As you can see, if the value in the matrix is None, no key has to be
added to the dictionary.

Of course, my tuple of tuples is a lot bigger.

How can I possibly do this?

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


Re: Building a dict from a tuple of tuples

2010-02-20 Thread MRAB

vsoler wrote:

Hello everyone!

I have a tuple of tuples, coming from an Excel range, such as this:

((None, u'x', u'y'),
(u'a', 1.0, 7.0),
(u'b', None, 8.0))

I need to build a dictionary that has, as key, the row and column
header.

For example:
d={ (u'a',u'x'):1.0, (u'a',u'y'): 7.0, (u'b',u'y'):8.0 }

As you can see, if the value in the matrix is None, no key has to be
added to the dictionary.

Of course, my tuple of tuples is a lot bigger.

How can I possibly do this?

Thank you


Does this help?

matrix = ((None, u'x', u'y'),
(u'a', 1.0, 7.0),
(u'b', None, 8.0))

for row in matrix[1 : ]:
for col, val in zip(matrix[0][1 : ], row[1 : ]):
print row[0], col, val
--
http://mail.python.org/mailman/listinfo/python-list


Re: Building a dict from a tuple of tuples

2010-02-20 Thread vsoler
On Feb 20, 7:00 pm, MRAB pyt...@mrabarnett.plus.com wrote:
 vsoler wrote:
  Hello everyone!

  I have a tuple of tuples, coming from an Excel range, such as this:

  ((None, u'x', u'y'),
  (u'a', 1.0, 7.0),
  (u'b', None, 8.0))

  I need to build a dictionary that has, as key, the row and column
  header.

  For example:
  d={ (u'a',u'x'):1.0, (u'a',u'y'): 7.0, (u'b',u'y'):8.0 }

  As you can see, if the value in the matrix is None, no key has to be
  added to the dictionary.

  Of course, my tuple of tuples is a lot bigger.

  How can I possibly do this?

  Thank you

 Does this help?

 matrix = ((None, u'x', u'y'),
 (u'a', 1.0, 7.0),
 (u'b', None, 8.0))

 for row in matrix[1 : ]:
      for col, val in zip(matrix[0][1 : ], row[1 : ]):
          print row[0], col, val

and the dictionary?

it is the ultimate goal of what I am intending...

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


Re: Building a dict from a tuple of tuples

2010-02-20 Thread MRAB

vsoler wrote:

On Feb 20, 7:00 pm, MRAB pyt...@mrabarnett.plus.com wrote:

vsoler wrote:

Hello everyone!
I have a tuple of tuples, coming from an Excel range, such as this:
((None, u'x', u'y'),
(u'a', 1.0, 7.0),
(u'b', None, 8.0))
I need to build a dictionary that has, as key, the row and column
header.
For example:
d={ (u'a',u'x'):1.0, (u'a',u'y'): 7.0, (u'b',u'y'):8.0 }
As you can see, if the value in the matrix is None, no key has to be
added to the dictionary.
Of course, my tuple of tuples is a lot bigger.
How can I possibly do this?
Thank you

Does this help?

matrix = ((None, u'x', u'y'),
(u'a', 1.0, 7.0),
(u'b', None, 8.0))

for row in matrix[1 : ]:
 for col, val in zip(matrix[0][1 : ], row[1 : ]):
 print row[0], col, val


and the dictionary?

it is the ultimate goal of what I am intending...

Thank you


The difficult bit is working out how to produce the keys and values for
the dict from the tuple of tuples, and I've shown you that. The rest is
straightforward.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Building a dict from a tuple of tuples

2010-02-20 Thread vsoler
On Feb 20, 8:54 pm, MRAB pyt...@mrabarnett.plus.com wrote:
 vsoler wrote:
  On Feb 20, 7:00 pm, MRAB pyt...@mrabarnett.plus.com wrote:
  vsoler wrote:
  Hello everyone!
  I have a tuple of tuples, coming from an Excel range, such as this:
  ((None, u'x', u'y'),
  (u'a', 1.0, 7.0),
  (u'b', None, 8.0))
  I need to build a dictionary that has, as key, the row and column
  header.
  For example:
  d={ (u'a',u'x'):1.0, (u'a',u'y'): 7.0, (u'b',u'y'):8.0 }
  As you can see, if the value in the matrix is None, no key has to be
  added to the dictionary.
  Of course, my tuple of tuples is a lot bigger.
  How can I possibly do this?
  Thank you
  Does this help?

  matrix = ((None, u'x', u'y'),
  (u'a', 1.0, 7.0),
  (u'b', None, 8.0))

  for row in matrix[1 : ]:
       for col, val in zip(matrix[0][1 : ], row[1 : ]):
           print row[0], col, val

  and the dictionary?

  it is the ultimate goal of what I am intending...

  Thank you

 The difficult bit is working out how to produce the keys and values for
 the dict from the tuple of tuples, and I've shown you that. The rest is
 straightforward.

I'll try. Thank you very much MRAB
-- 
http://mail.python.org/mailman/listinfo/python-list