473,666 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Looking for a different version of sort

I'm a Perl programmer learning Python (up to chapter 7 in Learning
Python, so go easy on me :-) and I find that I look to do things in
Python the way I would do them in Perl. In Perl functions and methods
usually only return and undefined value in the event of an error, make
an endless number of compound statements possible. Is there a version
of sort() I could import from somewhere that returns a reference to
the object on which it was performed, rather than returning "None".
t = ('x','y','z','a ','b','c',)
t ('x', 'y', 'z', 'a', 'b', 'c') list(t) ['x', 'y', 'z', 'a', 'b', 'c'] l = list(t).sort()
print l None l = list(t)
l.sort()
l ['a', 'b', 'c', 'x', 'y', 'z']
I would like "list(t).sort() " to return a new sorted list. For
example, then I could do "t = tuple(list(t).s ort())" to simulate an
in-place sort of a tuple assigned to the variable "t". When I try this
now I get:
t = tuple(list(t).s ort()) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: iteration over non-sequence


I assume this is because the "None" returned from sort() is the
non-sequence, but if sort had returned a reference to the list object
it was called upon, it would work.

Thanks,
Brian
Jul 18 '05 #1
3 1619
In article <8a************ **************@ posting.google. com>,
Bi*****@yahoo.c om (Brian McGonigle) wrote:
I would like "list(t).sort() " to return a new sorted list. For
example, then I could do "t = tuple(list(t).s ort())" to simulate an
in-place sort of a tuple assigned to the variable "t". When I try this
now I get:


There are situations when you might want to sort tuples, but they're
rare -- unless you need to use them as dict keys or something, it's more
likely that you should just be using lists.

But I think the actual answer to your question is that a sorted()
function is coming in Python 2.4 -- see e.g.
http://www.python.org/dev/doc/devel/whatsnew/node6.html
Once this is in place, you'd be able to do t = tuple(sorted(t) )
without even turning it into a list first.

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
Jul 18 '05 #2
In article <8a************ **************@ posting.google. com>,
Brian McGonigle <Bi*****@yahoo. com> wrote:
I'm a Perl programmer learning Python (up to chapter 7 in Learning
Python, so go easy on me :-) and I find that I look to do things in
Python the way I would do them in Perl. In Perl functions and methods

Jul 18 '05 #3
cl****@lairds.c om (Cameron Laird) wrote in message news:<10******* ******@corp.sup ernews.com>...
In article <8a************ **************@ posting.google. com>,
Brian McGonigle <Bi*****@yahoo. com> wrote:
I'm a Perl programmer learning Python (up to chapter 7 in Learning
Python, so go easy on me :-) and I find that I look to do things in
Python the way I would do them in Perl. In Perl functions and methods

.
.
.
Incidentally, is that the first or second edition you're reading?


First, thanks to all for the solutions. In another 40 pages I'll hit
the functions chapter and won't have to ask such newbie questions!

It's the second edition I'm reading, which covers Python 2.3. By the
way, what's the most common release? In Perl, I consider anything
older than 5.6 ancient and don't consider backwards compatability
beyond that point. Since programming is just a hobby, and I don't have
any customers, that's easy to do. For instance, I saw somewhere that
in 1.5 and prior, dir() wasn't available and you would have to use the
__method__ method or something similar to that. Should I worry about
stuff that old?
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4181
by: Jacinle Young | last post by:
Hi, I need to merge XML from different sources to 1 XML file. I am wondering if XSLT could do this job as usually it is used to perform 1 to 1 file transfrom eg. src1.xml <?xml version="1.0" encoding="UTF-8"?> <nickname>Fred</nickname>
8
2356
by: Sebastian Kerekes | last post by:
Greetings, I'm developing an application that supports multiple languages. In my XSL I use variables to place the text where it belongs to. At the top of the document I include those variables - the included file depends on the language. Atm I'm editing those file manually. Luckily atm it's only two languages I have to work with, but even in this case I forget to add a variable that I added in the other file, forget to use entities .....
2
1439
by: velthuijsen | last post by:
The STL sort only accepts a function that is defined in the form of bool Fname(<type>, <type>) in which <type> is the type of range to be sorted. I'm looking for a way to be able to sort the range on a different set of types then the range. I currently have worked out a way to do it (see code below) but it seems fairly involved and I was wondering if there is a better way of doing it?
2
4677
by: Robert Wille | last post by:
I have found that the sort order is different on Windows and Linux (same version of Postgres). On Windows, it sorts in what appears to be normal ASCIIorder. On Linux, lowercase comes before uppercase, and all non-alphanumerics appear to come before all alphanumerics. I really need it to sort on Linux the same as it does on Windows. The docs seem to imply that I can simplyset the LANG environment variable to C to get an ASCII sort order, but that...
6
1700
by: Pedro Alves | last post by:
Hi I'm having serious problems with a mission critical app that runs on postgres (and has been running for the past 3 years). It's rather large, and lately things are not going well. The planner is getting 'nuts', choosing ways that never end and completly stops the database. The strange thing is that running the same query with the same database
22
3299
by: Marc Mones | last post by:
Hello, I'working with IBM DB2 V8.1 and CLI/ODBC. I've got a problem with the following statement: ******************************************************************************** SELECT S_ART, S_SPRACHE, S_MANDANT, S_NR, S_SUB, S_OWNER, S_SATZ FROM SY0001_00005 WHERE S_ART = ? AND S_SPRACHE = ? AND S_MANDANT = ? AND S_NR = ? AND
1
2576
by: Johann Blake | last post by:
I am looking for a good solution on how to implement data access in an application so that there is a clean separation between the data access layer, the business layer and the GUI layer. I am looking for a robust solution for a major application. Almost every developer seems to come up with a completely different solution. While many of them are not bad, I really want a very good one. My database is SQL Server 2000 and I am using Visual...
1
5107
by: Chris H | last post by:
Im looking for a really easy to use and understand version of perls LWP function/module or whatver it is :-), Basically I am trying to fetch non RSS news from a couple websites that has news related to my sites content, all of the current scriptd ive come across are eitther for RSS/XML type news feeds, or made for a certain site, and or are just plain difficult for me to understand (still sort of a newbie to PHP, but an advanced newbie).....
1
2546
by: JpMaxMan | last post by:
Greetings. I'm having a strange problem on a Win 2003 server machine with VS 2005 installed. When I go to web site --asp.net configuration it loads the browswer window but instead of loading it gives me a 404 looking for login.aspx. Any help is appreciated. Server Error in '/asp.netwebadminfiles' Application. -------------------------------------------------------------------------------- The resource cannot be found. Description:...
0
8356
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,...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
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,...
0
8639
tracyyun
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...
0
5663
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();...
1
2769
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
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1772
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.