How to make the Join() method work for each word from multiple lists within another list

Asked

Viewed 53 times

-2

I had done something like this for the even and odd list for the question asks:

  • read several words (one by one)
  • and concatenate in two (2) texts the words read, as per:
  • TEXT 1: All PAR-Sized Words;
  • TEXT 2: All Odd-Sized Words;
  • Also concatene a hyphen: ?-' between each word of TEXT 1 and 2.

par = []
impar = []
a = ""
b = ""
while True:
    print('---MENU---')
    print('1- Palavra PAR / 2 - Palavra IMPAR/ 0 - SAIR')
    opcao = int(input())
    if opcao == 1:
        print('Digite uma palavra de tamanho PAR: ')
        palavra = (input())
        pal = palavra
        par.append(palavra)
    if opcao == 2:
        print('Digite uma palavra de tamanho IMPAR: ')
        palavra2 = input()
        pa = palavra2
        impar.append(palavra2)
    if opcao == 0:
        break

for i in range(len(par)):
    for j in range(i):
        for x in par[i][j]:
            a = '-'.join(par)

for r in range (len(impar)):
    for l in range(r):
        for z in par[r][l]:
            b ='-'.join(impar)

#printar resultados para usuário:
print(a)
print(b)

But he just imprinted something like

'abcd-acbd' if it has two even words and not: a-b-c-d, a-c-b-d

  • Could you give examples of what your programme’s inputs will be and what the outputs should be, explaining the context? Only with what you put in the question became impossible to understand the problem. Seek to elaborate a [mcve] to facilitate understanding.

  • I edited, improved?

1 answer

0


The problem is your last two for loops. You can do what you want by changing the loops, using lists and the method insert of lists. See:

par = []
impar = []
a = ""
b = ""
while True:
    print('---MENU---')
    print('1- Palavra PAR / 2 - Palavra IMPAR/ 0 - SAIR')
    opcao = int(input())
    if opcao == 1:
        print('Digite uma palavra de tamanho PAR: ')
        palavra = (input())
        pal = palavra
        par.append(palavra)
    if opcao == 2:
        print('Digite uma palavra de tamanho IMPAR: ')
        palavra2 = input()
        pa = palavra2
        impar.append(palavra2)
    if opcao == 0:
        break

lista_words_par = [] #essa será a lista de listas (a primeira dimensão é palavra e a segunda dimensão á letra)
par_split =[] #essa á a lista de sublistas (contendo as letras de cada palavra)
for word in par:
    for j in word:
        par_split.append(j)
    lista_words_par.insert(par.index(word),par_split) #inserindo a lista de letras que formam a palavra. Note que usamos o index da palavra na ordem que foi inserida pelo usuário na lista par
    par_split = [] #zera a lista de letras da palavra para usá-la novamente na próxima palavra

lista_words_impar = []
par_split =[]
for word in impar:
    for j in word:
        par_split.append(j)
    lista_words_impar.insert(impar.index(word),par_split)
    par_split = []

print('Palavras pares')
for i in lista_words_par:
    print('-'.join(i))

print('Palavras ímpares')
for i in lista_words_impar:
    print('-'.join(i))

See comments in code for more detail on how for loop was made

  • Hello, thank you very much, I learned new things from what you did, but what I wanted was: Also consider a hyphen: ? -' between each word of TEXT 1 and 2. That is, for each word even(2,4,6...) letters, hyphenate between them, for example: friends -> a-m-i-g-o-s

  • to do this just use the '-'join(i) in each print. I will edit the code to include this

  • Ah, sorry, I tried to do that, but I probably put Join in the wrong place, but thanks anyway

  • One more question, used the Insert instead of the append why? I do not know if I understood the comment well, but says it is to be in order? is not already in order automatically?

  • Really, in your case it makes no difference. The advantage of the insertjust control the order anyway

Browser other questions tagged

You are not signed in. Login or sign up in order to post.