Replace characters in a list with a column, not a row

I currently have the following lists:

counter = [13] instruments = ['3\t ---', '2\t / \\', '1\t / \\', '0\t--- \\ ---', '-1\t \\ /', '-2\t \\ /', '-3\t ---'] score = ['|*************|'] 

What I'm trying to do is replace the symbols in the tool list with symbols from the list of points (excluding | ).

I am currently having the following issues.

Characters are replaced row by row, not column by column.

Tool List:

 3 --- 2 / \ 1 / \ 0 --- \ --- -1 \ / -2 \ / -3 --- 

List of points:

 |*************| 

EXPECTED OUTPUT:

 3 *** 2 * * 1 * * 0 *** * -1 * -2 * -3 

Current output:

 3 *** 2 * * 1 * * 0 *** * ** -1 -2 -3 

Here's how I replace characters in the instruments list now:

 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 as to how I should correct my replacement character to make it go column-wise rather than row-wise.

+5
source share
2 answers

To solve your first problem, you need to iterate through the columns.

If you archive the lists (via itertools.zip_longest() , because they do not have the same length), you can 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 of repl.it , which displays:

 3 *** 2 * * 1 * * 0 *** * -1 * -2 * -3 

Note that it fills in lists with spaces, so you might want .strip() to get results if they are not just for printing. Adapting this to your account, I will leave you.

Another option that is probably understandable:

 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 

Since we truncate the rest of the list, it doesn't matter that replacing them all changes.

+3
source

Without itertools, instead of self-filling up to the longest part in the list:

 counter = [16] instruments = ['3\t ---', '2\t / \\', '1\t / \\', '0\t--- \\ ---', '-1\t \\ /', '-2\t \\ /', '-3\t ---'] score = ['|*************|'] # get longes part list maxL = max ( len(p) for p in instruments) #enlarge all to max length instrum2 = [k + ' '* (maxL-len(k)) for k in instruments] # mask out leading - to ~ (we reverse it later) instrum3 = [k if k[0] != '-' else '~'+''.join(k[1:]) for k in instrum2] # transpose and join to one lengthy sentence, #### are where we later split again trans = '####'.join(map(''.join,zip(*instrum3))) # replace the right amount of /-\ with * after that, replace with space instead cnt = 0 maxCnt = score[0].count('*') result = [] for t in trans: if t in '/-\\': if cnt < maxCnt: result.append('*') cnt+=1 else: result.append(' ') else: result.append(t) # resultlist back to string and split into columns again result2 = ''.join(result) trans2 = result2.split('####') # transpose back to rows and make - correct trans3 = [''.join(k).replace('~','-') for k in zip(*trans2 )] for p in trans3: print(p) 

Output:

 3 *** 2 * * 1 * * 0 *** * -1 * -2 * -3 
0
source

Source: https://habr.com/ru/post/1276287/


All Articles