Вот альтернативный способ очистки списка списков с использованием набора функций.
Сложная часть учитывала нарезку последней части списка, где есть чередующиеся строки, которые должны быть собраны в массив и отфильтрованы по пустым строкам.
Я предположил, что непустое строковое значение в первых 3 элементах хвостовой части каждого подмассива было желаемым значением. arrange
обрабатывает размещение первых 3 элементов в порядке, который возвращает согласованное значение.
ИМХО, преимущество этого способа в том, что если вы хотите сделать что-то другое с каким-либо конкретным элементом, вам будет проще изменить код.
import itertools as it
def get_first_char_int(item):
first_char, *_ = item
return int(first_char)
def identity(item):
return item
def get_floats(item):
tokens = ''.join(item.split(' ')[2:]).split('=')[1:]
return [float(token.split(';')[0]) for token in tokens]
def get_float(item):
return float(item) if item else item
UNCLEANED = [
['1 - 32/', 'Highway', '403', '43.167233',
'-80.275567', '1965', '2014', '2009', '4',
'Total=64 (1)=12;(2)=19;(3)=21;(4)=12;', '65', '04/13/2012', '72.3', '',
'72.3', '', '69.5', '', '70', '', '70.3', '', '70.5', '', '70.7', '72.9',
''],
['1 - 43/', 'WEST', '403', '43.164531', '-80.251582',
'1963', '2014', '2007', '4',
'Total=60.4 (1)=12.2;(2)=18;(3)=18;(4)=12.2;', '61', '04/13/2012',
'71.5', '', '71.5', '', '68.1', '', '69', '', '69.4', '', '69.4', '',
'70.3', '73.3', ''],
['2 - 4/', 'STOKES', '6', '45.036739', '-81.33579', '1958',
'2013', '', '1', 'Total=16 (1)=16;', '18.4', '08/28/2013', '85.1',
'85.1', '', '67.8', '', '67.4', '', '69.2', '70', '70.5', '', '75.1', '',
'90.1', ''],
]
functions = [ # 1:1 mapping of functions to items in each list in UNCLEANED.
get_first_char_int,
identity,
identity,
float,
float,
identity,
identity,
identity,
int,
get_floats,
float,
identity,
]
end = len(functions)
item_length, = {len(items) for items in UNCLEANED}
# Calculate argument to pass to it.islice
extra_count = item_length - end
# Extend functions by extra_count times with get_float
functions.extend(list(it.repeat(get_float, extra_count)))
#
# Handle items up to start of alternating strings and empty strings.
head_results = (
[f(item)
for f, item
in zip(functions[0:end], collection[0:end])]
for collection in UNCLEANED
)
def arrange(items):
"""Handle varying order of first 3 items of items."""
item, *_ = items
items[0:3] = [item, '', item]
return items
#
# Apply arrange to the tail of each sublist
collection_ = it.chain.from_iterable(arrange(collection[end:])
for collection in UNCLEANED)
#
# Handle items starting with alternating strings and empty strings.
tail_results = (
[f(item)
for f, item
in it.islice(zip(functions[end:], collection_), 2, item_length)]
for collection in UNCLEANED
)
results = [[head, [item for item in tail if item]]
for head, tail in zip(head_results, tail_results)]
for item in results:
print(item)
Выход:
[[1, 'Highway', '403', 43.167233, -80.275567, '1965', '2014', '2009', 4, [12.0, 19.0, 21.0, 12.0], 65.0, '04/13/2012'], [72.3, 69.5, 70.0, 70.3, 70.5, 70.7, 72.9]]
[[1, 'WEST', '403', 43.164531, -80.251582, '1963', '2014', '2007', 4, [12.2, 18.0, 18.0, 12.2], 61.0, '04/13/2012'], [71.5, 68.1, 69.0, 69.4, 69.4, 70.3, 73.3]]
[[2, 'STOKES', '6', 45.036739, -81.33579, '1958', '2013', '', 1, [16.0], 18.4, '08/28/2013'], [85.1, 67.8, 67.4, 69.2, 70.0, 70.5, 75.1, 90.1]]