Re: Clever way of sorting strings containing integers?

2008-10-09 Thread Holger
On 9 Okt., 09:41, Holger [EMAIL PROTECTED] 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.

 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.readlines()]
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]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Clever way of sorting strings containing integers?

2008-10-09 Thread Holger
On 9 Okt., 10:57, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] 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.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Clever way of sorting strings containing integers?

2008-10-09 Thread Marc 'BlackJack' Rintsch
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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Clever way of sorting strings containing integers?

2008-10-09 Thread bieffe62
On 9 Ott, 09:41, Holger [EMAIL PROTECTED] 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.

 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_function)


Ciao

FB


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