I have this code to weave the split_input ['5,4 4,5 8,7', '6,3 3,2 9,6 4,3', '7,6', '9,8', '5,5 7,8 6,5 6,4']
together. However I feel like this can be done more efficient (especially the weave function) but I have no clue how to improve it without importing any modules.
class Class1:
def __init__(self, row):
self.row = row
def append(self, coordinate):
self.row += [coordinate]
def extend(self, row):
for coordinate in row:
self.row += [coordinate]
def weave(self, row2):
result = Class1([])
for i in range(len(self.row)):
if i < len(self.row):
result.append(self.row[i])
if i < len(row2.row):
result.append(row2.row[i])
if len(row2.row) > len(self.row):
result.extend(row2.row[len(self.row):])
return result
def get_route(split_input):
rows = []
for i in range(len(split_input)):
rows += [Class1(split_input[i].split())]
previous_row = rows[0]
for row in rows[1:]:
previous_row = previous_row.weave(row)
return previous_row
woven_rows = get_route(split_input)
print woven_rows
Do you have any good advice?
The output of the code is one list. First, the first and second list are weaved into one list. Then this list is weaved with the third list etc.
print(woven_rows.row)
it shows['5,4', '5,5', '9,8', '7,8', '7,6', '6,5', '6,3', '6,4', '4,5', '3,2', '8,7', '9,6', '4,3']
. If that's the final result - why it should be namely like that and in that order? \$\endgroup\$[1, 2, 3, 4]
with["a", "b", "c", "d"]
should produce[1, "a", 2, "b", 3, "c", 4, "d"]
, and then you somehow weave that with["!", "@", "#", "$"]
, although I have no idea how that should look? Can you give some easier example in- and output? \$\endgroup\$