9 16776
Afanasiy: This is fairly simple in PHP, how do I do it in Python?
http://www.php.net/manual/en/function.ksort.php
def ksort(d, func = None):
keys = d.keys()
keys.sort(func)
return keys
for k in ksort(d):
print k, v
As a bonus, you don't need to tell the sort to sort numerically
vs. lexigraphically --- Python's strong typing knows that by
default. You can pass in an alternate compare function if you
want.
And no, I haven't tested it. ;)
Andrew da***@dalkescie ntific.com
Afanasiy: This is fairly simple in PHP, how do I do it in Python?
http://www.php.net/manual/en/function.ksort.php
def ksort(d, func = None):
keys = d.keys()
keys.sort(func)
return keys
for k in ksort(d):
print k, v
As a bonus, you don't need to tell the sort to sort numerically
vs. lexigraphically --- Python's strong typing knows that by
default. You can pass in an alternate compare function if you
want.
And no, I haven't tested it. ;)
Andrew da***@dalkescie ntific.com
On Sat, 23 Aug 2003 02:34:23 GMT, "Andrew Dalke" <ad****@mindspr ing.com>
wrote: Afanasiy: This is fairly simple in PHP, how do I do it in Python?
http://www.php.net/manual/en/function.ksort.php
def ksort(d, func = None): keys = d.keys() keys.sort(func) return keys
for k in ksort(d): print k, v
As a bonus, you don't need to tell the sort to sort numerically vs. lexigraphically --- Python's strong typing knows that by default. You can pass in an alternate compare function if you want.
Why wouldn't this be a standard function?
On Sat, 23 Aug 2003 02:34:23 GMT, "Andrew Dalke" <ad****@mindspr ing.com>
wrote: Afanasiy: This is fairly simple in PHP, how do I do it in Python?
http://www.php.net/manual/en/function.ksort.php
def ksort(d, func = None): keys = d.keys() keys.sort(func) return keys
for k in ksort(d): print k, v
As a bonus, you don't need to tell the sort to sort numerically vs. lexigraphically --- Python's strong typing knows that by default. You can pass in an alternate compare function if you want.
Why wouldn't this be a standard function?
Afanasiy: Why wouldn't [ksort] be a standard function?
Because it isn't needed all that often and can be built (when needed)
from the underlying primitives very simply. Because if there are a
lot of similar methods then it becomes harder to remember what each
one does.
The normal practice is
keys = d.keys()
keys.sort()
for k in keys:
....
which isn't all that onerous.
How about this one?
http://www.php.net/manual/en/function.asort.php
The normal idiom for sorting by value then printing
the key/value pairs is
rev_items = [(v, k) for k, v in d.items()]
rev_items.sort( )
for v, k in rev_items:
print k, v
If you want that as a function return just the keys
in value order
def asort(d):
rev_items = [(v, k) for k, v in d.items()]
rev_items.sort( )
return [k for (v, k) in rev_items]
As you can see, there are many ways you might want
to sort a dict. Why should all of them be present in
the standard dict type when it's really a matter of two
extra lines to get what you need. Seeing the code in
this case is much easier than memorizing the 7 different
sort functions mentioned in the PHP docs.
Additionally, Python's keys can be more complex than
a string or int. Eg,
d = {}
d[ (0,0) ] = "home"
d[ (1,3) ] = "school"
d[ (4,2) ] = "work"
y_items = [(y, name) for ((x, y), name) in d.items()]
y_items.sort()
for y, name in y_items:
print y, name
sorts by y position, ignoring x position. PHP doesn't
have a function for that, but it follows pretty naturally
from the idiomatically Python way to do it.
Andrew da***@dalkescie ntific.com
Afanasiy: Why wouldn't [ksort] be a standard function?
Because it isn't needed all that often and can be built (when needed)
from the underlying primitives very simply. Because if there are a
lot of similar methods then it becomes harder to remember what each
one does.
The normal practice is
keys = d.keys()
keys.sort()
for k in keys:
....
which isn't all that onerous.
How about this one?
http://www.php.net/manual/en/function.asort.php
The normal idiom for sorting by value then printing
the key/value pairs is
rev_items = [(v, k) for k, v in d.items()]
rev_items.sort( )
for v, k in rev_items:
print k, v
If you want that as a function return just the keys
in value order
def asort(d):
rev_items = [(v, k) for k, v in d.items()]
rev_items.sort( )
return [k for (v, k) in rev_items]
As you can see, there are many ways you might want
to sort a dict. Why should all of them be present in
the standard dict type when it's really a matter of two
extra lines to get what you need. Seeing the code in
this case is much easier than memorizing the 7 different
sort functions mentioned in the PHP docs.
Additionally, Python's keys can be more complex than
a string or int. Eg,
d = {}
d[ (0,0) ] = "home"
d[ (1,3) ] = "school"
d[ (4,2) ] = "work"
y_items = [(y, name) for ((x, y), name) in d.items()]
y_items.sort()
for y, name in y_items:
print y, name
sorts by y position, ignoring x position. PHP doesn't
have a function for that, but it follows pretty naturally
from the idiomatically Python way to do it.
Andrew da***@dalkescie ntific.com
On Sat, Aug 23, 2003 at 02:03:09AM +0000, Afanasiy wrote: This is fairly simple in PHP, how do I do it in Python?
In PHP, associative arrays are still regular arrays too, you can access
them by index (IIRC), and when you loop through them, they maintain the
order in which you assigned their items. Python seperates associative
arrays (dicts/hashes) from numerically indexed arrays (lists). You
can't sort a dict, because a dict has no order. You could do something
like:
mydict = { ..whatever.. }
sortedkeys = mydict.keys()
sortedkeys.sort ()
for key in sortedkeys:
print key, mydict[key]
Then we run into the issue of why we have to do list.sort() in place,
and I'm sure that's been discussed here a billion times (can't say I've
been part of any of those discussions though).
--
m a c k s t a n n mack @ incise.org http://incise.org
After a few boring years, socially meaningful rock 'n' roll died out.
It was replaced by disco, which offers no guidance to any form of life
more advanced than the lichen family.
-- Dave Barry, "Kids Today: They Don't Know Dum Diddly Do" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: KraftDiner |
last post by:
I have two lists.
I want to sort by a value in the first list and have the second list
sorted as well... Any suggestions on how I should/could do this?
|
by: Shi Mu |
last post by:
Got confused by the following code:
>>> a
>>> b
>>> c
{1: , ], 2: ]}
>>> c.append(b.sort())
>>> c
{1: , ], 2: , None]}
|
by: Markus Franz |
last post by:
Hi!
I have:
x = {'a':3, 'b':2, 'c':4}
How can I sort x by value? (I tried using sorted() with x.items() - but I
didn't get a dictionary as response.)
My second question:
|
by: max sharma |
last post by:
Hi all,
I am using a hashtable for my application. Its similar to word count
application. How can I sort the hashtable w.r.t.
the VALUE and not the KEY. The sample data of the table is given below
which is sorted according the URL as in Dictionary:
www.webroot.com (11)
www.webshots.com (1)
www.weddingprints.com (4)
here, Key = URL (e.g. www.webroot.com)
|
by: spohle |
last post by:
hi i have a normal dictionary with key and value pairs. now i wanna
sort by the keys BUT in a specific order i determine in a list !? any
ideas
dic = {'key1':'value1', 'key2':'value2', 'key3':'value3'}
list =
| |
by: Neil Chambers |
last post by:
Hi All,
I'm looking to see if it's feasible to use the SortObjectCommand
included in the Microsoft.Powershell.Commands assembly.
I have a Dictionary
Dictionary<string, intd = new Dictionary<string, int>();
d = new int;
|
by: kdt |
last post by:
Hi,
I need to perform some horrible functions in python I need to do, using sort in a similar way that Excel can.
With a dictionary like:
>>> d
{8: (99, 99), 9: , 4: , 5: (67, 77)}
I want to sort the entire dictionary based on the last values in each line. First for and then.
|
by: pravinasp |
last post by:
Hello there
Iam stuck trying to sort the dictionary object in classic asp. Am trying to sort by the values of the dictionary object and not using keys, and once sorted by values i need the keys according to the sorted values. The new order of keys would then be used to fetch records from DB. Hope am clear. if i can do something like that without using multidimensional arrays I'd be glad. Could anyone here please shed some light..???
Tons...
|
by: NvrBst |
last post by:
I have a log viewer. I sort the DataGridView by the Time Column and
then run a function to set all cell backcolors depending if the cell
above is different.
This works correctly, however, when I resort a column (by clicking any
of the column headers) then all the colors revert back to the
origional white background.
Is there a way to have the DefaultCellStyle for each cell follow the
value/cell on sort?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |