0

I'm trying to create a range between two variables. The variables contain string and number characters.

For example P9160-P9163 or P360-P369. The P is not constant and could be any character(s)/multiple, but i'm trying to generate a list that would contain all values in between.

i tried with looking at ASCII characters but didn't work for me.

Any thoughts?

x = 'P9160'
y = 'P9163'

x = re.match(r"([a-z]+)([0-9]+)", x, re.I)
y = re.match(r"([a-z]+)([0-9]+)", y, re.I)

for i in range(int(x.groups()[1]), int(y.groups()[1])+1):
    print("{}{}".format(x.groups()[0], i))
  • 1
    Would you please share what you have tried so far? Hit the "edit" button above and paste in the code you've tried and why it does not work. This will help everyone understand the approach(es) you've already taken. – SyntaxRules Apr 22 '19 at 18:04
  • I did something like this... which seems pretty long x = 'P9160' y = 'P9163' x = re.match(r"([a-z]+)([0-9]+)", x, re.I) y = re.match(r"([a-z]+)([0-9]+)", y, re.I) for i in range(int(x.groups()[1]), int(y.groups()[1])+1): print("{}{}".format(x.groups()[0], i)) – aversalus01 Apr 22 '19 at 18:15
  • 1
    @aversalus01 Please [edit] to add details. Comments don't support all the formatting necessary for representing code. – wjandrea Apr 22 '19 at 18:21
  • 1
    Could you also [edit] and give an example of what you mean by *"The P is not constant and could be any character(s)/multiple"*? – wjandrea Apr 22 '19 at 18:24
  • What is not constant... The "P" could be any other letter or multiple letters... for example "GX", "XYZ"... etc... so not necessary just one character. I'm just looking for something faster than the above code I supplied – aversalus01 Apr 22 '19 at 18:28
  • 1
    Please also add more details on the expected output: you say P could also be another letter but the few examples you give all contain only P. Are there a minimum and maximum length? – theberzi Apr 22 '19 at 18:45
  • No minimum or maximum lengths: Here are examples: J120-J189, LX8944-LX8945, M8080-M8088. – aversalus01 Apr 22 '19 at 19:04
  • @aversalus01 So the letter portion is always capital letters of matching length? – wjandrea Apr 22 '19 at 19:11
  • Related: [range over character in python](https://stackoverflow.com/q/7001144/4518341) – wjandrea Apr 22 '19 at 19:14
  • @aversalus01 So all you want is faster code that generates exactly the same result from your code, correct? – Khalid Ali Apr 22 '19 at 19:27
  • @KhalidAli, Yes, faster code than what I have above. – aversalus01 Apr 22 '19 at 19:41

1 Answers1

0

Using a reusable regex pattern, and a generator expression, does certainly improves the code performance.

import re

x = 'P9160'
y = 'P9173'

# resuable regex pattern
regex = re.compile(r"([a-zA-Z]+)(\d+)")

x, y = regex.match(x), regex.match(y)

# generator expression
xy = (x.groups()[0]+str(i) for i in range(int(x.groups()[1]), int(y.groups()[1])+1))

# list of all values from the generator
print(list(xy))
Khalid Ali
  • 1,224
  • 1
  • 8
  • 12