Currently I have the following lists:
counter = [13]
instruments = ['3\t ---', '2\t / \\', '1\t / \\', '0\t--- \\ ---', '-1\t \\ /', '-2\t \\ /', '-3\t ---']
score = ['|*************|']
What I am trying to do is to replace the characters in the instruments list with the characters from the score list (excluding the |
).
I am currently experiencing the following issues
The characters are being replaced row by row, rather than column by column.
Instrument List:
3 ---
2 / \
1 / \
0 --- \ ---
-1 \ /
-2 \ /
-3 ---
Score List:
|*************|
EXPECTED OUTPUT:
3 ***
2 * *
1 * *
0 *** *
-1 *
-2 *
-3
Current Output:
3 ***
2 * *
1 * *
0 *** * **
-1
-2
-3
This is how I am currently replacing the characters in the instruments
list:
for elements in counter:
current_counter = elements
count = 0
for elements in instrument_wave:
amplitude, form = elements.split('\t')
for characters in form:
if characters in ['-', '/', '\\']:
form = form.replace(characters, '*', 1)
count += 1
if count == current_counter:
break
for characters in form:
if characters in ['-', '/', '\\']:
form = form.replace(characters, '')
if '-' not in amplitude:
amplitude = ' ' + amplitude
new_wave = amplitude + "\t" + form
waveform.append(new_wave)
Any help would be appreciated, especially with regards to how I should fix my replace character to make it go column by column rather than row by row.
Pandas replace multiple values in column replace. By using DataFrame. replace() method we will replace multiple values with multiple new strings or text for an individual DataFrame column. This method searches the entire Pandas DataFrame and replaces every specified value.
We can replace characters using str. replace() method is basically replacing an existing string or character in a string with a new one. we can replace characters in strings is for the entire dataframe as well as for a particular column.
You can replace a string in the pandas DataFrame column by using replace(), str. replace() with lambda functions.
You can replace values of all or selected columns based on the condition of pandas DataFrame by using DataFrame. loc[ ] property. The loc[] is used to access a group of rows and columns by label(s) or a boolean array. It can access and can also manipulate the values of pandas DataFrame.
To solve your first issue, you need to iterate via columns.
If you zip the lists (via itertools.zip_longest()
, as they are not all the same length), you can then go through them in order and truncate the result:
import itertools
cols = list(itertools.zip_longest(*lst, fillvalue=" "))
for i in range(3, 17): # skip negative signs
cols[i] = "".join(cols[i]).replace('-', '*', 1)
cols[i] = "".join(cols[i]).replace('/', '*', 1)
cols[i] = "".join(cols[i]).replace('\\', '*', 1)
fixed = map("".join, zip(*cols[:17])) # no need to zip longest
for l in fixed:
print(l)
See a working example on repl.it, which outputs:
3 ***
2 * *
1 * *
0 *** *
-1 *
-2 *
-3
Note it does pad the lists out with spaces, so you may want to .strip()
the results if it isn't just for printing. Adapting that to your score input I'll leave up to you.
Another option, which is probably clearer:
def convert_and_truncate(lst, cutoff):
result = []
for str in lst:
str = str[0] + str[1:].replace('-', '*') # skip the negative signs
str = str.replace('/', '*')
str = str.replace('\\', '*')
result.append(str[:cutoff]) # truncate
return result
Because we're truncating the rest of the list, it doesn't matter that replace is changing them all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With