4

I am writing a python script that will accept a dot-delimited version number. It will split this string into individual components (using period (.) as a delimiter). My script supports up to 4 components (e.g. 1.2.3.4). However, the user may specify less or more components than 4 and my script needs to be able to handle it.

If less than 4, the list that I get back from string.split() needs to be resized to 4 and missing components initialized to the string "0". If greater than 4, just truncate the elements past the 4th component.

The truncation is easy, I would just splice the list. However, I'm not sure how to perform the resize up to 4 elements. Is there a pythonic way of doing this, or do I need to hand-write a bunch of logic to do it?

I'm using Python 3.2.

void.pointer
  • 24,859
  • 31
  • 132
  • 243

4 Answers4

3

You can generally check the length of the list and just add the missing list back to the start:

def pad_list(input_string, pad_length, pad_char='0'):
    base_version = input_string.split('.')[:pad_length]
    missing_entries = [pad_char] * pad_length - len(base_version)
    return base_version + missing_entries

Really the only addition here is to actually check the length of the partial list you're getting and then create another list with the same length as the missing section.

Alternately, you could just add the 0's ahead of time in case it's too short:

(a + '.' + '.'.join(['0'] * 4)).split('.')[:4]

The idea behind the second line here is to preemptively add zeros to your version string, which effectively eliminates your padding issue.

First, we add '.' + '.'.join(['0']*4) to the end of the string a, this adds .0.0.0.0 to the end, which means that effectively the padding is there regardless of what's in the rest of the string.

After the padding is there, you slice as you normally would: .split('.')[:4]

Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144
2

This does the trick

def GetVersionList( version_string ):
    vlist = version_string.split('.')[:4]
    vlist.extend('0' * (4 - len(vlist) ) )
    return vlist

Or a more general version

def FixedLengthListFromString( input, length, delimiter='.', padChar='0' ):
    vlist = input.split(delimiter)[:length]
    vlist.extend(padChar * (length - len(vlist) ) )
    return vlist
wallacer
  • 12,855
  • 3
  • 27
  • 45
  • 1
    Maybe people downvote because the solution doesn't look very "pythonic", but I don't care as long as it works. Not everything has to be done in one line of code! – void.pointer Jun 07 '14 at 01:54
  • Some of the downvotes may have been for the PascalCase and strange use of spaces. –  Jan 29 '18 at 03:29
1
>>> (['0'] * 4 + "4.5".split('.'))[-4:]
['0', '0', '4', '5']
>>> (['0'] * 4 + "4.5".split('.'))[-4:]
['0', '0', '4', '5']
>>> (['0'] * 4 + "2.4.5".split('.'))[-4:]
['0', '2', '4', '5']
>>> (['0'] * 4 + "1.2.4.5".split('.'))[-4:]
['1', '2', '4', '5']
Ivan Burlutskiy
  • 1,605
  • 1
  • 12
  • 22
-1

If your list is a and you want to pad it out to 4 elements

 a = a + [0]*(4-len(a))

Will do the padding you require.

This works because with any list l, l * x will create a new list with the element of l repeated x times so [0]*4 is just the list [0,0,0,0]. And len(a) is the current length of your list so 4-len(a) is the number of extra elements required.

Jack Aidley
  • 19,439
  • 7
  • 43
  • 70