-1

My code is something like below

for i in re.finditer('someinteger(.+?)withanother(.+?)', html):
    x = i.group(1)
    y = i.group(2)

Here x and y related to each other. If I want to index them in an array or something and sort the index based on x how do i approach. Considering index length is only 4 or 5. Meaning (i) will loop for max 4-5 times.

A quick code would be help. No clue with multi arrays.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
Abul Hasnat
  • 1,541
  • 2
  • 16
  • 23
  • 4
    Are you parsing HTML with regex... – TerryA Jul 02 '13 at 12:04
  • Obligatory SO link: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Esenti Jul 02 '13 at 12:06
  • considering regex html is not going to be an issue... – Abul Hasnat Jul 02 '13 at 12:08
  • 1
    @Esenti: That link is getting really old, provides incorrect and unhelpful information and has nothing to do with this question. Could people please stop this knee-jerk reaction every time someone mentions "HTML" and "regex" in a post? – Tim Pietzcker Jul 02 '13 at 12:15

1 Answers1

3

You can first retrieve the values into a list. re.findall() will do that automatically:

values = re.findall(r'someinteger(.+?)withanother(.+?)', html)

Then you can sort the list:

values.sort()

if you want to sort by x (in your example).

For example:

>>> s = "someinteger5withanother1someinteger4withanother2someinteger3withanother3"
>>> values = re.findall(r'someinteger(.+?)withanother(.+?)', s)
>>> values
[('5', '1'), ('4', '2'), ('3', '3')]
>>> values.sort()
>>> values
[('3', '3'), ('4', '2'), ('5', '1')]

Of course you're still dealing with strings, if you want to sort numerically, you either need to do

values = [(int(x), int(y)) for x,y in values]

to convert them all to integers, or do

values.sort(key=lambda x: int(x[0]))
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561