python merge lines
by sharky from LinuxQuestions.org on (#6CS3S)
I have to use python because it will be part of existing python code.
Example input
Quote:
Desired output
Quote:
The concept seems simple, just combine dev#s that have devTypes, but the solution eludes me.
Below is the monstrosity that I tried and it fails miserably. In the below example 'regressionList' corresponds to the above sample input.
Code:def writeTest(regressionList):
# extract all cellNames
cellsToPlace = []
for test in regressionList:
if test[0] not in cellsToPlace:
cellsToPlace.append(test[0])
for cell in cellsToPlace:
devTypes = []
for test in regressionList:
if test[0] == cell and len(test) > 1:
devTypes.append(test[2])
print(cell,devTypes)
Example input
Quote:
[ ['dev1', 'devType', 'x1'] ['dev1', 'devType', 'x2'] ['dev1', 'devType', 'x3'] ['dev2', 'devType', 'y1'] ['dev2', 'devType', 'y2'] ['dev2', 'devType', 'y3'] ['dev2', 'devType', 'y4'] ['dev2', 'devType', 'y5'] ['dev3'] ['dev4'] ['dev5', 'devType', 'z1'] ['dev5', 'devType', 'z2'] ] |
Quote:
[ ['dev1', 'devType x1:x2:x3'] ['dev2', 'devType y1:y2:y3:y4:y5'] ['dev3'] ['dev4'] ['dev5', 'devType z1:z2'] ] |
Below is the monstrosity that I tried and it fails miserably. In the below example 'regressionList' corresponds to the above sample input.
Code:def writeTest(regressionList):
# extract all cellNames
cellsToPlace = []
for test in regressionList:
if test[0] not in cellsToPlace:
cellsToPlace.append(test[0])
for cell in cellsToPlace:
devTypes = []
for test in regressionList:
if test[0] == cell and len(test) > 1:
devTypes.append(test[2])
print(cell,devTypes)