Inspired by R P Herrold's challenge.

Some ways of splitting data in Python follow.


In [1]:
MONTH_NDAYS = ''' 
    0:31
    1:29
    2:31
    3:30
    4:31
    5:30
    6:31
    7:31
    8:30
    9:31
    10:30
    11:31
'''.split()

In [2]:
MONTH_NDAYS


Out[2]:
['0:31',
 '1:29',
 '2:31',
 '3:30',
 '4:31',
 '5:30',
 '6:31',
 '7:31',
 '8:30',
 '9:31',
 '10:30',
 '11:31']

In [3]:
for month_n_days in MONTH_NDAYS:
    month, n_days = map(int, month_n_days.split(':'))
    print(f'{month} has {n_days}')


0 has 31
1 has 29
2 has 31
3 has 30
4 has 31
5 has 30
6 has 31
7 has 31
8 has 30
9 has 31
10 has 30
11 has 31

Tuple unpacking is nice and in this case enables DRY (aka Single Source of Truth) code. Compare:

month, n_days = month_n_days.split(':')

with

FUTLHS=`echo "$j" | awk -F: {'print $1'}`
FUTRHS=`echo "$j" | awk -F: {'print $2'}`

One can do all the chopping up front. The chopping code is less readable, but the subsequent for loop is just pretty.


In [4]:
MONTH_NDAYS = [list(map(int, s.split(':'))) for s in ''' 
    0:31
    1:29
    2:31
    3:30
    4:31
    5:30
    6:31
    7:31
    8:30
    9:31
    10:30
    11:31
'''.split()]

In [5]:
MONTH_NDAYS


Out[5]:
[[0, 31],
 [1, 29],
 [2, 31],
 [3, 30],
 [4, 31],
 [5, 30],
 [6, 31],
 [7, 31],
 [8, 30],
 [9, 31],
 [10, 30],
 [11, 31]]

In [6]:
for month, n_days in MONTH_NDAYS:
    print(f'{month} has {n_days}')


0 has 31
1 has 29
2 has 31
3 has 30
4 has 31
5 has 30
6 has 31
7 has 31
8 has 30
9 has 31
10 has 30
11 has 31

Unnesting the chopping code makes it easier to read. The for loop is still pretty.


In [7]:
MONTH_NDAYS = ''' 
    0:31
    1:29
    2:31
    3:30
    4:31
    5:30
    6:31
    7:31
    8:30
    9:31
    10:30
    11:31
'''.split()
MONTH_NDAYS


Out[7]:
['0:31',
 '1:29',
 '2:31',
 '3:30',
 '4:31',
 '5:30',
 '6:31',
 '7:31',
 '8:30',
 '9:31',
 '10:30',
 '11:31']

In [8]:
MONTH_NDAYS = [s.split(':') for s in MONTH_NDAYS]
MONTH_NDAYS


Out[8]:
[['0', '31'],
 ['1', '29'],
 ['2', '31'],
 ['3', '30'],
 ['4', '31'],
 ['5', '30'],
 ['6', '31'],
 ['7', '31'],
 ['8', '30'],
 ['9', '31'],
 ['10', '30'],
 ['11', '31']]

In [9]:
MONTH_NDAYS = [list(map(int, x)) for x in MONTH_NDAYS]
MONTH_NDAYS


Out[9]:
[[0, 31],
 [1, 29],
 [2, 31],
 [3, 30],
 [4, 31],
 [5, 30],
 [6, 31],
 [7, 31],
 [8, 30],
 [9, 31],
 [10, 30],
 [11, 31]]

In [10]:
for month, n_days in MONTH_NDAYS:
    print(f'{month} has {n_days}')


0 has 31
1 has 29
2 has 31
3 has 30
4 has 31
5 has 30
6 has 31
7 has 31
8 has 30
9 has 31
10 has 30
11 has 31