Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expanding a block of numbers in Python

Before I asked, I did some googling, and was unable to find an answer.

The scenario I have is this: A list of numbers are passed to the script, either \n-delimited via a file, or comma-delimited via a command line arg. The numbers can be singular, or in blocks, like so:

File:

1
2
3
7-10
15
20-25

Command Line Arg:

1, 2, 3, 7-10, 15, 20-25

Both end up in the same list[]. I would like to expand the 7-10 or 20-25 blocks (obviously in the actual script these numbers will vary) and append them onto a new list with the final list looking like this:

['1','2','3','7','8','9','10','15','20','21','22','23','24','25']

I understand that something like .append(range(7,10)) could help me here, but I can't seem to be able to find out which elements of the original list[] have the need for expansion.

So, my question is this: Given a list[]:

['1','2','3','7-10','15','20-25'],

how can I get a list[]:

 ['1','2','3','7','8','9','10','15','20','21','22','23','24','25']
like image 960
Shadow Master Avatar asked May 12 '15 20:05

Shadow Master


People also ask

How to increase numbers in a list in Python?

There are four methods to add elements to a List in Python. append() : append the element to the end of the list. insert() : inserts the element before the given index. extend() : extends the list by appending elements from the iterable.

What is 1e 4 in Python?

Python takes the number to the left of the e and multiplies it by 10 raised to the power of the number after the e . So 1e6 is equivalent to 1×10⁶. The literal 1e-4 is interpreted as 10 raised to the power -4 , which is 1/10000, or 0.0001 .

How do you add unlimited numbers in Python?

In Python, there is no way or method to represent infinity as an integer. This matches the fundamental characteristic of many other popular programming languages. But due to python being dynamically typed language, you can use float(inf) as an integer to represent it as infinity.


2 Answers

Input:

arg = ['1','2','3','7-10','15','20-25']

Output:

out = []
for s in arg:
    a, b, *_ = map(int, s.split('-') * 2)
    out.extend(map(str, range(a, b+1)))

Or (in Python 2):

out = []
for s in arg:
    r = map(int, s.split('-'))
    out.extend(map(str, range(r[0], r[-1]+1)))
like image 141
Stefan Pochmann Avatar answered Oct 26 '22 00:10

Stefan Pochmann


So let's say you're given the list:

L = ['1','2','3','7-10','15','20-25']

and you want to expand out all the ranges contained therein:

answer = []
for elem in L:
    if '-' not in elem:
        answer.append(elem)
        continue
    start, end = elem.split('-')
    answer.extend(map(str, range(int(start), int(end)+1)))

Of course, there's a handy one-liner for this:

answer = list(itertools.chain.from_iterable([[e] if '-' not in e else map(str, range(*[int(i) for i in e.split('-')]) + [int(i)]) for e in L]))

But this exploits the nature of leaky variables in python2.7, which I don't think will work in python3. Also, it's not exactly the most readable line of code. So I wouldn't really use it in production, if I were you... unless you really hate your manager.

References:  append()  continue  split()  extend()  map()  range()  list()  itertools.chain.from_iterable()  int()

like image 30
inspectorG4dget Avatar answered Oct 26 '22 01:10

inspectorG4dget