I need to replace digits in list alphabet starting from index key_m in alphabet of digits in list m_b by index in list c1
from operator import setitem
def replace():
alphabet = [['a'], ['b'], ['c'], ['d'], ['e'], ['f'],
['g'], ['h'], ['i'], ['j'], ['k'], ['l'],
['m'], ['n'], ['o'], ['p'], ['q'], ['r'],
['s'], ['t'], ['u'], ['w'], ['v'], ['x'],
['y'], ['z']]
key_m = 10
c1 = [17, 10, 21, 22, 1]
m_b = [['y'], ['a'], ['#'], ['b'], ['o'], ['i'],
['c'], ['d'], ['e'], ['f'], ['g'], ['h'],
['j'], ['k'], ['l'], ['m'], ['n'], ['p'],
['q'], ['r'], ['s'], ['t'], ['u'], ['v'],
['w'], ['x'], ['z'], ['_'], ['-'], ['!'],
['?'], ['$'], ['/'], ["@"], [' '], ['%']]
for b, c in zip(c1, m_b):
setitem(alphabet, b, c)
print(alphabet)
expected result is alphabet = [['a'], ['b'], ['c'], ['d'], ['e'], ['f'], ['g'], ['h'], ['i'], ['j'], ['p'], ['g'],['t'], ['u'], ['a'], ['p'], ['q'], ['r'], ['s'], ['t'], ['u'], ['w'], ['v'], ['x'],['y'], ['z']]
but the actual result is alphabet = [['a'], ['o'], ['c'], ['d'], ['e'], ['f'], ['g'], ['h'], ['i'], ['j'], ['a'], ['l'], ['m'], ['n'], ['o'], ['p'], ['q'], ['y'], ['s'], ['t'], ['u'], ['#'], ['b'], ['x'], ['y'], ['z']]
CodePudding user response:
Use a loop to iterate on c1
values, wrap in enumerate
to have indices increasing (starting at key_m
) then set the values
key_m = 10
c1 = [17, 10, 21, 22, 1]
for idx, value in enumerate(c1, start=key_m):
alphabet[idx] = m_b[value]
I maintain that using 1d list and string would be nicer
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'w', 'v', 'x', 'y', 'z']
m_b = 'ya#boicdefghjklmnpqrstuvwxz_-!?$/@ %'
key_m = 10
c1 = [17, 10, 21, 22, 1]
for idx, value in enumerate(c1, start=key_m):
alphabet[idx] = m_b[value]
print(alphabet)
# ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'p', 'g', 't',
# 'u', 'a', 'p', 'q', 'r', 's', 't', 'u', 'w', 'v', 'x', 'y', 'z']