Python List Coding Doubt

Can somebody suggest a way in python to replace only alphabetic characters from first list with second list alphabets ??
(alphabets are always in uppercase)

L1=[’@’, ‘T’, ‘U’, ‘U’, ‘W’, ‘Y’, ’ ', ‘D’, ‘Y’, ‘U’, ‘@’, ’ ', 9,9]

L2=[‘X’, ‘W’, ‘O’, ‘P’, ‘K’, ‘A’, ‘X’, ‘B’]

Expected output:
[’@’, ‘X’, ‘W’ ,‘O’, ‘P’, ‘K’, ’ ’ , ‘A’, ‘X’, ‘B’, ‘@’, ’ ’ ,9 ,9]

i tried doing it by first converting L1 to string,then reversed it,
L1. reverse()
L1=str(L1)

iterated over L1 and by put condition if( ord(i)>=65 and ord(i) <=90) then
L1= L1. replace(i, L2. pop(), 1)

but then even though special character position is correct in output… the characters are not in same order as the expected output

How will this convert it into a string?
Share your code.

sorry did an error while typing in post… Edited the post

How is this even useful?
If you think this convert L1 into something like “@TU…” , then you’re wrong.
Again,

plainText=input("Enter Plain Text: ")
plainText=plainText.upper()
################------------REMOVE SP. CHARS----------################
pt=""
for i in plainText:
    if(ord(i)>=65 and ord(i)<=90):
        pt+=i

print("Removing sp. chrs: ",pt)
################------------KEY GENERATION----------################

prefix=""
suffix=""

for i in range(len(pt)//2):
    prefix+=secrets.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
print("Prefix is: ",prefix)


rand_pt=random.sample(str(pt),(len(pt)-len(prefix)))

for i in rand_pt:
    suffix+=i
print("Suffix is: ",suffix)


Autokey=prefix+suffix
print("Autokey Generated: ",Autokey)

################------------ENCRYPTION-----------################

pt=list(pt)
ak=list(Autokey)
ct_list=[]

for (a,b) in zip(pt,ak):
    if(ord(a)>=65 and ord(a)<=90):
        ct_list.append(chr(((ord(a)-65)+(ord(b)-65))%26+65))
    else:
        ct_list.append(a)
print("cipherText: ",ct_list)



pt2=plainText[::-1]
for i in pt2:
    if(ord(i)>=65 and ord(i)<=90):
        pt2=pt2.replace(i,ct_list.pop(),1)
        #print(pt2)
        #print(ct_list)

print("Encrypted Text: ",pt2[::-1])

The last part is where i want to print my encrypted text pt2 along with special characters as it in plain text…in that part i am facing issue …if possible please help

The output and logic is correct.
For this input @TUUWY DYU@ 99
and cipherText as ['X', 'W', 'O', 'P', 'K', 'A', 'X', 'B']
I know cipherText is randomized but for the sake to check the output, I changed it in between your code.
I get this this output @XWOPK AXB@ 99 which is correct.
Your code is correct.

Actually i am unable to share the image of my output… But i wasn’t getting expected output when random key is generated… So is there any other approach to get same output… Bcoz random key generation is mandatory

plainText="@TUUWY DYU@ 99"
ct_list=['X', 'W', 'O', 'P', 'K', 'A', 'X', 'B']
counter=0
pt2=list(plainText)
for i in range(len(pt2)):
    if (ord(pt2[i])>=65 and ord(pt2[i])<=90):
        pt2[i]=ct_list[counter]
        counter+=1
print(pt2)
Output- ['@', 'X', 'W', 'O', 'P', 'K', ' ', 'A', 'X', 'B', '@', ' ', '9', '9']

I don’t know how are you facing issues in previous code but this is easier to understand and definitely correct.

1 Like

Yes it worked… Thanks a lot!!

1 Like

Why would you convert that list to a string? why would you use extra space?

Here you go with a better solution.

L1=['@', 'T', 'U', 'U', 'W', 'Y',' ', 'D', 'Y', 'U', '@',' ', 9,9]
L2=['X', 'W', 'O', 'P', 'K', 'A', 'X', 'B']

pos1 = 0
pos2 = 0

while pos1 < len(L1) and pos2 < len(L2):
  if str(L1[pos1]).isalpha():
    while pos2 < len(L2) and str(L2[pos2]).isalpha():
      pos2 += 1
      break;
    L1[pos1] = L2[pos2 - 1]
  pos1 += 1
print(L1)

P.S = to convert a list L to string do something like ''.join(str(i) for i in L)
you have to convert each element to str.

1 Like

Yes, i tried this approach… N its working too… Thank you…The benefit of this approach is mainly i can even avoid special characters from list 2 from interfering while merging.