running split() to form a list within another list

Asked

Viewed 2,424 times

4

have the following list:

[['a:/, b:/, w:/, g:/, f:/, d:/Downloads/Torrent/End, x:/files.2t, y:/files.1t'], ['d:/Dropbox/project/rato_bat'], ['data']]

however I would like it to be a list within another list so I did:

    for a in lldir:
        for b in a:
           b = b.split(', ')

it would be possible to do this on a line and assign to a variable?

  • You want to produce a list only from all other lists inside?

  • this list already has a list inside it only that this list is being formed by a single string that I would like to be divided into multiple items using the ", " (comma and space) of the string.

  • 1

    Voce has had the same kind of questions since yesterday - if you’re not already doing this, I suggest fortemten that you try a little with examples of the data that Voce has at hand in the interactive Python prompt (as you are in Windows, use the idle ) - you have a case where we can give you hundreds of examples, and they can work on time, but nothing will be so clear to you to think about it in interactive mode.

1 answer

4


You want something like:

novo_conjunto = []
for a in lldir:
    nova_lista = [] 
    for b in a:
       nova_lista.append(b.split(', '))
    novo_conjunto.append(nova_lista)
a = novo_conjunto

If you want in one line:

a = [[elemento.split()] for elemento in lista] for lista in a]

If you want a single list of all directory entries, from all sublists of the first entry:

diretorios = []
for lista in a:
    for elemento in lista:
        for diretorio in elemento.split(","):
            diretorio.append(diretorio.strip())

Browser other questions tagged

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