This is a Python function. It’s convert to a list of words from a string. But I don’t understand the algorithmic the part of code:
for c in ch:
if c==" ":
lista.append(ct) # add to list of temporary string
ct="" # the ch temporary string reinicialization
I don’t understand, how the ct can append to the list, because the ct=””? How casn it get value?
Thanks for the help!
Here the full code of function:
def szoLista(ch):
"a ch karakterláncot átalakítja szavakból álló listává"
lista, ct=[],"" # ct átmeneti string
for c in ch:
if c==" ":
lista.append(ct) # a listához adjuk a ch átmenei stringet
ct="" # a ch átmeneti string reinicializálása
else:
ct=ct+c
if ct !="":
lista.append(ct) # az utolsó szó hozzáadása
return lista
These are the lines you are overlooking.
ctis set empty when a space if found, but after it was already added to the list.If the current character is not a space, the character is appended to
ct.So when you encounter the next space, you have the last word in
ctand can add it to the list.