I tried to do this elegantly, but did not come up with a good solution
Sort strings like
foo1bar2
foo10bar10
foo2bar3
foo10bar2
So that they come out:
foo1bar2
foo2bar3
foo10bar2
foo10bar10
I.e. isolate integer parts and sort them according to integer value.
Thx
Holger 4 2432
On 9 Okt., 09:41, Holger <ish...@gmail.c omwrote:
I tried to do this elegantly, but did not come up with a good solution
Sort strings like
foo1bar2
foo10bar10
foo2bar3
foo10bar2
So that they come out:
foo1bar2
foo2bar3
foo10bar2
foo10bar10
I.e. isolate integer parts and sort them according to integer value.
Thx
Holger
or even:
foo1bar2
foo10bar10
foo2bar3
foo10bar2
fo
bar1000
777
To:
777
bar1000
fo
foo1bar2
foo2bar3
foo10bar2
foo10bar10
Here's my own take, but it doesn't quite work yet:
txtline = re.compile(r'(\ D*)(\d+)')
lines = [x.strip() for x in sys.stdin.readl ines()]
res = []
for l in [ x for x in lines if x]:
groups = txtline.findall (l)
res.append([[[x[0], int(x[1],0)] for x in groups],l])
res.sort()
for x in res:
print x[1]
On Thu, 09 Oct 2008 00:41:27 -0700, Holger wrote:
I tried to do this elegantly, but did not come up with a good solution
Sort strings like
foo1bar2
foo10bar10
foo2bar3
foo10bar2
So that they come out:
foo1bar2
foo2bar3
foo10bar2
foo10bar10
I.e. isolate integer parts and sort them according to integer value.
import re
def key_func(string ):
result = re.split(r'(\d+ )', string)
for i in xrange(1, len(result), 2):
result[i] = int(result[i])
return result
def main():
lines = ['foo1bar2',
'foo10bar10',
'foo2bar3',
'foo10bar2',
'fo',
'bar1000',
'777']
lines.sort(key= key_func)
print '\n'.join(lines )
Ciao,
Marc 'BlackJack' Rintsch
On 9 Ott, 09:41, Holger <ish...@gmail.c omwrote:
I tried to do this elegantly, but did not come up with a good solution
Sort strings like
foo1bar2
foo10bar10
foo2bar3
foo10bar2
So that they come out:
foo1bar2
foo2bar3
foo10bar2
foo10bar10
I.e. isolate integer parts and sort them according to integer value.
Thx
Holger
This should work, if you have all stribngs in memory:
import re
REXP = re.compile( r'\d+' )
lines = ['foo1bar2', 'foo10bar10', 'foo2bar3', 'foo10bar2' ]
def key_function( s ): return map(int, re.findall(REXP , s ))
lines.sort( key=key_functio n)
Ciao
----
FB
On 9 Okt., 10:57, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
On Thu, 09 Oct 2008 00:41:27 -0700, Holger wrote:
I tried to do this elegantly, but did not come up with a good solution
Sort strings like
foo1bar2
foo10bar10
foo2bar3
foo10bar2
So that they come out:
foo1bar2
foo2bar3
foo10bar2
foo10bar10
I.e. isolate integer parts and sort them according to integer value.
import re
def key_func(string ):
* * result = re.split(r'(\d+ )', string)
* * for i in xrange(1, len(result), 2):
* * * * result[i] = int(result[i])
* * return result
def main():
* * lines = ['foo1bar2',
* * * * * * *'foo10bar10',
* * * * * * *'foo2bar3',
* * * * * * *'foo10bar2',
* * * * * * *'fo',
* * * * * * *'bar1000',
* * * * * * *'777']
* * lines.sort(key= key_func)
* * print '\n'.join(lines )
Perfect!
Thank you. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: dont bother |
last post by:
This is really driving me crazy.
I have a dictionary feature_vectors{}. I try to sort
its keys using
#apply sorting on feature_vectors
sorted_feature_vector=feature_vectors.keys()
sorted_feature_vector.sort()
#feature_vector.keys()=sorted_feature_vector
|
by: C. Barnes |
last post by:
Summary:
Sorts strings in a way that seems natural to humans.
If the
strings contain integers, then the integers are
ordered
numerically. For example, sorts
into the order .
Code:
|
by: Connelly Barnes |
last post by:
Summary:
Sorts strings in a way that seems natural to humans.
If the strings contain integers, then the integers are ordered
numerically. For example, sorts
into the order .
Code:
#---------------------------------------------------------
|
by: mike |
last post by:
If I had a date in the format "01-Jan-05" it does not sort properly
with my sort routine:
function compareDate(a,b)
{
var date_a = new Date(a);
var date_b = new Date(b);
if (date_a < date_b)
{ return -1; }
else
|
by: Scott |
last post by:
I have a collection where the items in the collection are dates. I want to
iterate over the collection and build a value list string for the rowsource
of a listbox. The dates in the collection are not in chronological order. Is
there a way to first sort the collection and put the dates in chronological
order before creating the value list string? Or, how would I iterate over
the collection pulling out the dates in chronological order?
...
| |
by: melanieab |
last post by:
Hi,
When you click on a column header to sort in ascending or descending order,
numbers (and dates) aren't sorted correctly. It turns up like this:
1
10
11
2
3
4
|
by: ReMEn |
last post by:
My question is this:
How can I apply some kind of a function to a listview that allows it to sort
by both string and numbers whenever a header is clicked?
For example:
-------------------
INDEX
-------------------
|
by: Kittyhawk |
last post by:
I would like to sort an Arraylist of objects on multiple properties. For
instance, I have a Sort Index property and an ID property (both integers).
So, the results of my sort would look like this:
sort index, id
1000,1
1000,2
1000,3
1001,1
1001,2
|
by: DierkErdmann |
last post by:
Hi !
I know that this topic has been discussed in the past, but I could not
find a working solution for my problem: sorting (lists of) strings
containing special characters like "ä", "ü",... (german umlaute).
Consider the following list:
l =
For sorting the letter "Ä" is supposed to be treated like "Ae",
therefore sorting this list should yield
|
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: 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...
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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: 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
| |