Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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’]]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading