-2

I have a list of string

line = ["set_param(find_system('Fccroot_to_ICD','RegExp','on','FindAll','on','Name','B10_Stat_TRXA_FCC'),'port', '1218');",
"set_param(find_system('Fccroot_to_ICD','RegExp','on','FindAll','on','Name','Az_INS3_RawValidity'),'port', '1438');"]

I am trying to sort is based on last digit 1438 and 1218 sorting numerically ..'1438' should be treated as number

below lines doesn't give me expected result

line = sorted(line)

["set_param(find_system('Fccroot_to_ICD','RegExp','on','FindAll','on','Name','Az_INS3_RawValidity'),'port', '1438');",
 "set_param(find_system('Fccroot_to_ICD','RegExp','on','FindAll','on','Name','B10_Stat_TRXA_FCC'),'port', '1218');"]
Hariom Singh
  • 3,512
  • 6
  • 28
  • 52

5 Answers5

2

You can use a regex to find the last occurrence of digits surrounded by single quotes in the key argument of sorted:

import re

sorted(line,key=lambda x: int(re.findall("'(\d+)'", x)[-1]))
sacuL
  • 49,704
  • 8
  • 81
  • 106
1

You could extract the sub-string from the last comma onwards and then extract the digits from that string.

sorted(line, key=lambda s: int(''.join(c for c in s.split(',')[-1] if c.isdigit())))

giving

["set_param(find_system('Fccroot_to_ICD','RegExp','on','FindAll','on','Name','B10_Stat_TRXA_FCC'),'port', '1218');", 
"set_param(find_system('Fccroot_to_ICD','RegExp','on','FindAll','on','Name','Az_INS3_RawValidity'),'port', '1438');"]
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
1

Assuming your data stays in that format:

>>> sorted(line, key=lambda x: int(x.split()[-1][1:-3]))
["set_param(find_system('Fccroot_to_ICD','RegExp','on','FindAll','on','Name','B10_Stat_TRXA_FCC'),'port', '1218');",
 "set_param(find_system('Fccroot_to_ICD','RegExp','on','FindAll','on','Name','Az_INS3_RawValidity'),'port', '1438');"]

I takes each line, splits it, and then takes the last item (e.g. "'1218');" for the first line). It then strips the first character (') and the last three (');) via slice notation. Finally, it converts this text value to an integer and uses it as a key for sorting.

A regex solution is more general.

sorted(line, key=lambda x: int(re.findall(r'(\d+)', x.split()[-1])[0]))

This will fail if the last item in the split does not contain any digits.

Alexander
  • 105,104
  • 32
  • 201
  • 196
0

Use python regex (https://docs.python.org/3.6/library/re.html) to extract the integer value at the end of the strings, then you can create a function which takes your string as an argument, and returns the integer.

Finally, used a lambda to the key argument to the sorted method to pass in the function. Python will used your function as the key to sort with. (like this: Syntax behind sorted(key=lambda: ...) )

user2886057
  • 646
  • 1
  • 5
  • 15
0

Not as good as @Alexander response, same result but extra step

new = sorted(line, key=lambda x: int((x.split()[-1]).split("'")[1]))
(xenial)vash@localhost:~/python/stack_overflow$ python3.7 list.py

["set_param(find_system('Fccroot_to_ICD','RegExp','on','FindAll','on','Name'
,'B10_Stat_TRXA_FCC'),'port', '1218');", 
"set_param(find_system('Fccroot_to_ICD','RegExp','on','FindAll','on','Name',
'Az_INS3_RawValidity'),'port', '1438');"]
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20