'Python: Join list with dynamic/changing glue (join with some form of callback)
I'm trying to join a list of strings, but with a changing string as "glue" and was wondering if there is an elegant way to do this. So in short I've a list ['A','B','C','D'] and want to join it into something like "A_x1_B_x2_C_x3_D".
For the background: I'm building a "complex filter" to pass on to ffmpeg. The way these filters are constructed is that the output of each command is "stored" in a "variable" and than the next command takes this as an input. And those "variables" should be unique. So a final string looks something like "[0]some_command[a1];[a1]next_command[a2];[a2]third_command[output]" - the input is treated with "some_command" and piped into a1, a1 is than treated with "next_command" and so on...
In my python-program I've stored all the commands I want to apply in an list and now need to somehow stitch it together. But I'm completely lost on how to join such a list with those changing "variables". My last resort would be to do it in a loop by hand and with some "if"'s somehow make sure that the first variable is [0] and the last is [output]. But this seems so messy and I was wondering if there is some nicer/more "pythonic" way to do this. Some form of "magic join" that would take the return of a function as glue or something like that.
def joinerCallback():
global i
i += 1
return "[a%i]:[a%i]" % (i,i)
i = 0
mylist=['filter_command_A','filter_command_B','filter_command_C']
string=joinerCallback().join(mylist) #Doesn't work, but is there a way to somehow dynamically change the string that glues everything together
print("[0]%s[output]" % string)
# Should print [0]filter_command_A[a1]:[a1]filter_command_B[a2]:[a2]filter_command_C[output]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
