0
Play this code and read output. I asked my question there.
4 Réponses
+ 6
One-liner:
print("".join(list(map(lambda x:dic[x.upper()], ow))))
+ 4
The print function has an end property, which is default to "\n".
You can change it like this:
print(ow, end = "")
+ 4
For me there are some more doubts in this code. May be its worth to get what the code is doing:
Here a revised code with some friendly comments:
dic = {"N": "1", "A": "2", "M": "3", "E": "4",}
ow = "Name" #**does not need brackets
list1 = []
for i in ow.upper():
#i = i.upper()
if i in dic:
#ow = (dic.get(i)) #**(1) during current iteration in ow this variable is changed by this code line. This is should not be.
#list1.append(ow) #**(2) ow, now containing numbers is appended to list1
list1.append(dic.get(i)) #**(3) This the correct way to pic values from the dict and append it to list1
print("".join(list1))
EDIT:
https://code.sololearn.com/cS6xZRr7Qh87/?ref=app
+ 3
#Changing keyword argument "end" is the easiest way, but here is another way:
dic = {"N": "1", "A": "2", "M": "3", "E": "4",}
ow = ("Name")
list1 = []
for i in ow:
i = i.upper()
if i in dic:
ow = (dic.get(i))
list1.append(ow)
print("".join(list1))
print("\nThe output as u see is into 4 lines how can output be 1234 in one single line?")