On 20 Nov 2005 21:12:52 -0800, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:

>
>Bengt Richter wrote:
>> On Sun, 20 Nov 2005 22:03:34 +0100, Christoph Zwerschke <[EMAIL PROTECTED]> 
>> wrote:
>> >> Ordering the keys isn't the normal case, and can be done easily when
>> >> needed.
>> >
>> >That depends. Maybe I do not want the keys to be sorted alphabetically,
>> >but according to some criteria which cannot be derived from the keys
>> >themselves.
>> You mean involving also the values? What's wrong with
>>     sorted(plaindict.items(), key=your_ordering_function) ?
>>
>Not according to the content of the data, not just the "key". Or in
>other words, some other metadata that is not present in the data. A
>typical thing, like order of creation. Or some arbitary order. For
>example :
>
>I present a data grid/table in a HTML form and the user just drag and
>drop and rearrange the columns order.
          ^^[1]
[1] implies known info of before and after rearrangement. Where do these
come from, and are the two states expressed as ordered sets of keys generated 
and stored somewhere?
The point is, to re-order, you need a mapping from unordered data dict keys to 
values which the sorted
builtin function will order in the way you want. (BTW, if you use DSU, make 
sure the data is not modifying
your sort in an undesired way. Passing a key function to sorted makes it easy 
to exclude unwanted data from
the sort). If you have data that determines a new ordering of keys, it has to 
be accessed somehow, so
you just need to make it accessible to a handy helper that will generate your 
key function. E.g,
with before and after lists of keys expressing e.g. drag-drop before and after 
orderings, lambda can do the
job of getting you dict items in the new order, e.g., where bef and aft are 
lists that define the desired orderings
before and after in the sense of sort_precedence = bef.index[key_in_bef] and 
same for aft.

    sorted(thedict.items(),key=lambda t:dict(zip(bef,((k in aft and 
aft.index(k) or len(aft)+bef.index(k)) for k in bef))[t[0]])

Ok, that one-liner grew a bit ;-)
>
>Of course, you may say, just put another column that represent
>this(some reporting programs I have seen do it this way) and that is an
>option but not the only option.
>
Maybe you could keep the rearranged_keys vector in a per-user cookie, if it's a 
web app
and amounts to a user personalization?

( posting delayed >12 hrs due to news server prob ;-/ )

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

Reply via email to