1

I want to have yourfeedback. I need to process an input which is a range defined as string:

targets = 'machine[7:10]'

The result that I need is:

results = ['machine07', 'machine08', 'machine09', 'machine10']

Here is what I have coded in Python:

for target in targets:

    m = re.search('\\d+:\d+\]', target)
    if m:
        name = target.split('[')[0]
        target_range = re.findall('[0-9]+', target)
        print(target_range)
        target_range = [int(i) for i in target_range]
        for tgt in range(target_range[0],target_range[1]+1):
             results.append(name + str(format(tgt, '02d')))

Please, can you tell me is there is a more elegant way or pythonic way of doing the same thing ? ... with NO import module! Thank you.

3
  • 3
    "with no import module" - isn't re an imported module? Commented Feb 10, 2017 at 13:01
  • I'm guessing they mean "nothing which needs an extra library to be installed" Commented Feb 10, 2017 at 13:04
  • Yep my bad import re is allowed Commented Feb 10, 2017 at 13:08

7 Answers 7

3

Capture the desired substrings in 3 groups. Generate a range object by the last 2 groups by converting them to int.

Then run a for loop on the range object and append to the first group.

You can try something like this:

import re

targets = 'machine[7:10]'
a,b,c = re.findall(r'(.*?)\[(\d+):(\d+)\]', targets)[0]
res = [a+'{:0>2}'.format(d) for d in range(int(b),int(c)+1)]
print res

Output:

['machine07', 'machine08', 'machine09', 'machine10']
Sign up to request clarification or add additional context in comments.

Comments

1

You can use groups:

> targets = 'machine[7:10]'
> m = re.match(r'(\w+)\[(\d+):(\d+)\]', targets)
> results = ['{}{:0>2}'.format(m.group(1), str(i)) for i in range(int(m.group(2)), int(m.group(3))+1)]
> results
['machine07', 'machine08', 'machine09', 'machine10']

Comments

0

You already used split() in your code, why not just take that a step farther and eliminate the need for re?

(base, indices) = targets.split('[')
(start, end) = indices[:-1].split(':')

target_range = []
for i in range(int(start), int(end)+1):
    target_range.append('{0}{1:02}'.format(base, i))

Comments

0

Try this,

word,nums_ = targets.split('[')
nums = map(int,nums_.replace(']','').split(':'))
nums[-1] = nums[-1]+1
print [word+'{:0>2}'.format(i) for i in range(*nums)]

result

['machine07', 'machine08', 'machine09', 'machine10']

Comments

0

List comprehension combined with some string splicing is one way to do it without importing anything:

targets = 'machine[7:10]'

start_idx = targets.index('[')
separator_idx = targets.index(':')
end_idx = targets.index(']')

name = targets[:start_idx]
range_start = int(targets[start_idx + 1:separator_idx])
range_end = int(targets[separator_idx + 1:end_idx]) + 1

results = [name + "{:02}".format(i) for i in range(range_start, range_end)]

print(results)

3 Comments

Hang on a moment I accidentally hit submit. I'm in the midst of editing my answer.
OK fine but you are using hardcoded value ... such as machine and 7,11 ! I should have specified that from the string itself you need to extract the information. What if my string is : server[1:25] !
@Ep1cF4iL Refresh to see my solution please.
0

First split into range and target:

target, ranges = targets[:-1].split('[')

Then get low and high range:

range_min, range_max = [int(i) for i in ranges.split(':')]

Then loop over range and create string:

results = ['{}_{:0>2}'.format(target, i) for i in range(range_min, range_max + 1)]

results:

['machine_07', 'machine_08', 'machine_09', 'machine_10']

Comments

0

I prefer something more readable, try to use the new way of formatting strings, it makes everything much clear.

targets = 'machine[7:10]'
results = []

name, range_ = targets.split('[')
idx = [int(x) for x in range_[:-1].split(':')]

for i in range(idx[0],idx[1]+1):
    if i < 10:
        results.append(f'{name}0{i}')
    else:
        results.append(f'{name}{i}')

>>>results
>>>['machine07', 'machine08', 'machine09', 'machine10']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.