I have two objects, one which is a list of tuples with (int, str), like this:
first_input = [
(0 , "Lorem ipsum dolor sit amet, consectetur"),
(1 , " adipiscing elit"),
(0 , ". In pellentesque\npharetra ex, at varius sem suscipit ac. "),
(-1 , "Suspendisse luctus\ncondimentum velit a laoreet. "),
(0 , "Donec dolor urna, tempus sed nulla vitae, dignissim varius neque.")
]
# Note that the strings contain newlines `\n` on purpose.
The other object is a string, which is the result of a series of operations(*) which, by design, will result in a concatenation of all the strings above but with some additional newlines \n inserted.
(* : that can't be done while conserving the list of tuples structure, obviously)
For instance:
second_input = "Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit. In pellentesque\npharetra ex, at varius sem\nsuscipit ac. Suspendisse luctus\ncondimentum velit a laoreet. Donec dolor urna, tempus sed\nnulla vitae, dignissim varius neque."
# Note that there are 3 new newlines, here ^ for instance
# but also in "sem\nsuscipit" and "sed\nnulla"
My goal is to go back to the first structure, but keeping the additional newlines. So in my example, I would get:
expected_output = [
(0 , "Lorem ipsum dolor sit amet,\nconsectetur"), # new newline here
(1 , " adipiscing elit"),
(0 , ". In pellentesque\npharetra ex, at varius sem\nsuscipit ac. "), # new newline here
(-1 , "Suspendisse luctus\ncondimentum velit a laoreet. "),
(0 , "Donec dolor urna, tempus sed\nnulla vitae, dignissim varius neque.") # new newline here
]
Do you have a smart way to do it, other than reconstructing the string with a character by character comparison?
(NB: I don't care in which of the two tuples it ends if a new \n is at the limit of a string. E.g. getting [(0, "foo\n"), (1, "bar")] or [(0, "foo"), (1, "\nbar")] doesn't matter.)
Edit: what I want to avoid, is to do something like this:
position=0
output = []
for tup in first_input:
reconstructed_string = ""
for letter in tup[1]:
if letter == second_input[position]:
reconstructed_string = reconstructed_string + letter
else:
reconstructed_string = reconstructed_string + second_input[position]
position +=1
output.append((tup[0], reconstructed_string))
# Note: this is hastily written to give you an idea, I have no idea if it would work properly, probably not
# Well, it does seem to work without bug, at least in my example. That's unexpected lol. Anyway, if you can think of a better solution...!
That is, going through each character of the strings and compare them to reconstruct the strings character by character.