How to transform a list [['a'], ['b']] into a string: ab

Asked

Viewed 136 times

0

quantidade = int(input())
lista = []
lista2 = []
lista3 = []
palavra =()
i = 0
while quantidade != len(lista):
    a = input()
    lista.append(a)
for x in lista:
    b = x.split()
    lista2.append(b)
for x in lista2:
    x[0] = int(x[0])
del(lista[0:quantidade - 1])
lista2.sort()
for x in lista2:
    del(x[0])
    lista3.append(x)
print(lista3)

problem link: https://www.thehuxley.com/problem/2197/code-editor/? quizId=3169

  • 1

    It is a question ? If yes what is the question ? The code does not work ? It does not do what is expected ? It gives error ?

  • List [['a'], ['b']] or matrix [['a'], ['b']]?

2 answers

2

(Answering the title, because the rest did not understand anything, please edit and improve the question)

From a list, we can concatenate its values in various ways such as:

lista = ['o','l','a']

string = ''.join(lista)
string = ''.join([x for x in lista])
string = ''.join(map(lambda x: x, lista))

def concatena(lista):
    string = ''
    for x in lista:
        string += x
    return string    
string = concatena(lista)

>>> print(string)
ola

And we can even modify if necessary:

string = ''.join([x.upper() for x in lista]) #ou .lower()

Complicating a little, but not too much, we can play with matrices. Now just take the matrix lists and apply a method above.

matriz = [['o'],['l'],['a']]

#string = ''.join(map(lambda lista: Algum_metodo_acima, matriz))
#string = ''.join([Algum_metodo_acima for lista in matriz])

string = ''.join(map(lambda lista: ''.join(lista), matriz))
string = ''.join([''.join(lista) for lista in matriz])

def concatena_matriz(matriz):
    string = ''
    for lista in matriz:
        for x in lista:
            string += x
    return string

string = concatena_matriz(matriz)

>>> print(string)
ola

1

If I understand your question, you want to extract the data from a list and string it, if that’s it, there are several to do, one of them is using Listcomps.

alfabeto = [['a'], ['b'], ['c']]

''.join([letras for lista in alfabeto 
                for letras in lista])

Output: 'abc'

Browser other questions tagged

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