1
The output of my code makes a list in a column.
Code:
path = '/home/allan/Área de trabalho/adjetivos.txt'
i = 0
lista = []
conta = 1
with open (path) as file_obj:
ler = file_obj.readlines()
for le in ler:
#print(le.rstrip())
lista.append(le)
for i in lista:
tam = len(lista)
#print(tam)
if i == lista[0]:
print("1 ano: Bodas de "+str(i.title().rstrip()))
elif i != lista[0]:
conta = 1 + conta
print(str(conta)+" anos: Bodas de "+i.title().rstrip())
Current output:
1 ano: Bodas de Ágata
2 anos: Bodas de Água-Marinha
3 anos: Bodas de Âmbar
4 anos: Bodas de Alabastro
5 anos: Bodas de Alexandrita
6 anos: Bodas de Amazônia
7 anos: Bodas de Ametista
8 anos: Bodas de Andaluzite
9 anos: Bodas de Aventurina
10 anos: Bodas de Axinite
...
...Thus continues
I would like to "break the text", that the list continues beside as in the image:
How can I do?
for texto in textos:
print(max(len(texto)))
Why can’t I have the same effect of counting the characters as you did using listcomp? My interpretation of the code working on pythontutor is that 2 items on the list form 1, Ex: "coffee","sugar" -> "sugar coffee". It seems magical, how he manages to include himself?
Here:
Is this how it happens? See below:
escritas[pos % linhas_coluna]
would be like that:
...
[3 % 25]
[4 % 25]
[5 % 25]
...
So I’d take the 3
and would assign to anos: Bodas de Âmbar (espaço de 25)
texto.ljust(tamanho, ' ')
would be:
anos: Bodas de Âmbar (espaço de 25)
,right?
I can’t see this text going right, just to ljust(left) thus forming a single column!
Remembering: The program is working perfectly well, but I can’t reach the logic itself. How can he split in two if everything goes left??
I pointed out 3 doubts. I accepted but did not understand why the string 'size' returns 43.
tamanho = max([len(texto) for texto in textos]) + 1
Here where I don’t understand anymore:for pos, texto in enumerate(textos):
 escritas[pos % linhas_coluna] += texto.ljust(tamanho, ' ')
My interpretation: ’s writing[From 0 to 49 % 25 ] <-- I don’t get it, 50 % 25 = 0 =+ <-- Add where? text.ljust(43, ' ') #text with 43 left-justified spaces , an empty space ' '. <-- Besides the size being 43 I didn’t understand the empty.– Allan Belem
@Allanbelem size returns
43
because it considers the texts already constructed as "1 year: Marriage of Agata" rather than just the adjectives. In+= texto.ljust(tamanho, ' ')
we are adding the current text aligned to the left with size43
, and if you have less add spaces to the right until you43
. Inescritas[pos % linhas_coluna]
happens that to50
elements and2
columns will givepos % 25
which will be a number between0
and24
always. It is a way to ensure that it does not exceed24
and always returns to 0 being circular. If you see the editing history see another way to do the same– Isac
@Allan If you want to be even more aware of how the module operator works in this case, observe the code and the output of this programme. Another thing that would be interesting is to visualize the code in my answer on python online viewer which is quite intuitive and gives you a very visual sense of what is happening
– Isac
I am studying your code, I still find myself confused, I would like to know something else about the format of your code, the way it is described summarizes the functions in a single line, there is a technical name for this representation? I want to learn this way, since the advanced/intermediate books teach the way you presented them.
– Allan Belem
@Allanbelem If you’re referring to something like
lista = [le.strip() for le in ler]
then yes, it’s called List Comprehensions. So I advise you to read this documentation article to get more familiar with how it works. The main idea however to make code shorter, simpler and readable– Isac
@Allanbelem I re-edited my answer by placing a writing function in fully manual columns, to be as simple as possible without using native functions. See if you can understand the code and if it makes sense, preferably by looking at pythontutor as well. If you have questions do not hesitate to ask.
– Isac