Let's consider that I have a list like the following one:
myList = [
'1000', 'ParameterName=Device type', 'ObjectType=0x7', 'DataType=0x7',
'AccessType=ro', 'PDOMapping=0', 'ObjFlags=1', 'ParameterValue=0x00020192',
'1001', 'ParameterName=Error register', 'ObjectType=0x7', 'DataType=0x5',
'AccessType=ro', 'PDOMapping=0', 'ObjFlags=1', 'ParameterValue=0x00',
'1003', 'SubNumber=6', 'ParameterName=Error history', 'ObjectType=0x8'
]
My split points will be '1000', '1001' and '1003' where the objective is to have
listoflists = [
['1000', 'ParameterName=Device type', 'ObjectType=0x7', 'DataType=0x7',
'AccessType=ro', 'PDOMapping=0', 'ObjFlags=1', 'ParameterValue=0x00020192'],
['1001', 'ParameterName=Error register', 'ObjectType=0x7', 'DataType=0x5',
'AccessType=ro', 'PDOMapping=0', 'ObjFlags=1', 'ParameterValue=0x00'],
['1003', 'SubNumber=6', 'ParameterName=Error history', 'ObjectType=0x8']
]
I can easily do it with a simple for loop as follows.
There are some extra checks as I asked for the nominal case but the number can be an hex (so I use is_numeric) and also it can be with a different format such as 4digit+string (1234sub2) so I slice the data. The last check is because as I use the is_digit some hex can be considered data when it is really text but as always that his happens there is an 'DataThatMaybeIsConfusedAsHex = value' I can discriminate using the '='.
for value in configurationFileList:
if is_numeric(value[:4]) and counter != 0 and "=" not in value:
# Append the list to the list of lists
configurationFileListForEachIndex.append(tmpList.copy())
# Clear the list
tmpList.clear()
# Append the New Index
tmpList.append(value)
else:
tmpList.append(value)
counter += 1
I would like to ask if there is any 'prettier' and more efficient way of doing this.