Separate each phrase word in an index from the python list

Asked

Viewed 8,207 times

3

This code snippet generates me the following output.

tamanho = len(lista_nome_base_docs)
print (tamanho)
lista_geral_de_lista_arquivos = []


for i in range(tamanho):
with open(lista_nome_base_docs[i],"r") as arquivo:
    conteudo = arquivo.readlines()
    lista_geral_de_lista_arquivos.append(conteudo)

print (lista_geral_de_lista_arquivos)

Exit:

[['Do mesmo modo, a consulta aos diversos militantes acarreta um processo de 
reformulacao e modernizacao do fluxo de informacoes.'], ['Evidentemente, a 
determinacao clara de objetivos promove a alavancagem dos niveis de 
motivacao departamental.'], ['Gostaria de enfatizar que a percepcao das 
dificuldades cumpre um papel essencial na formulacao das diretrizes de 
desenvolvimento para o futuro.']]

that is a list list.. but in the internal list the sentence is inside a list position ['phrasing'] like I make her look this way ['sentence,'whole']

Would someone tell me some way to do this or scroll through this string within the list position...

  • 2

    It is the fourth question you open related to this subject. Wouldn’t it be easier for you to describe exactly what you need to do and we’ll help you through the whole process?

  • @Andersoncarloswoss is because I’m doing a job.. and venturing into python.... and if I’m going to put the problem as a whole, I don’t think it would be a good idea, because each one would structure the problem differently, and my problem itself is not structuring the problem, but how to manipulate some structures, so I’m removing doubts in parts.

  • Then just use the split to separate the phrase into words.

  • @Andersoncarloswoss it is.. that I already knew... the problem is how to do this by getting a list from inside another list... for example, I’m drafting this here: for numbers in lista_geral_de_lista_arquivos:
 for number in numbers:
 conteudo_string = lista_geral_de_lista_arquivos[number].split()
 lista_palavras.append(conteudo_string) but it hasn’t worked yet

3 answers

4


You have a list of lists with only one string. Semantically, this makes very little, if anything, sense, so maybe the way you’re creating these lists isn’t the best way. But if the intention is to have a list of the words of each sentence, to modify the list you currently have, just do:

lista = [
    ['Do mesmo modo, a consulta aos diversos militantes acarreta um processo de reformulacao e modernizacao do fluxo de informacoes.'], 
    ['Evidentemente, a determinacao clara de objetivos promove a alavancagem dos niveis de motivacao departamental.'], 
    ['Gostaria de enfatizar que a percepcao das dificuldades cumpre um papel essencial na formulacao das diretrizes de desenvolvimento para o futuro.']
]

for i, frase in enumerate(lista):
    lista[i] = frase[0].split()

print(lista)

See working on Ideone | Repl.it

So you will have a word list:

[
    ['Do', 'mesmo', 'modo,', 'a', 'consulta', 'aos', 'diversos', 'militantes', 'acarreta', 'um', 'processo', 'de', 'reformulacao', 'e', 'modernizacao', 'do', 'fluxo', 'de', 'informacoes.'], 
    ['Evidentemente,', 'a', 'determinacao', 'clara', 'de', 'objetivos', 'promove', 'a', 'alavancagem', 'dos', 'niveis', 'de', 'motivacao', 'departamental.'], 
    ['Gostaria', 'de', 'enfatizar', 'que', 'a', 'percepcao', 'das', 'dificuldades', 'cumpre', 'um', 'papel', 'essencial', 'na', 'formulacao', 'das', 'diretrizes', 'de', 'desenvolvimento', 'para', 'o', 'futuro.']
]
  • exactly what I’m trying to do here!

1

1

On this issue just go to phrase as parameter of the list. For this we should simply implement the following code below...

frase_inteira = list(input('Digite a frase: ').split())
print(frase_inteira)

Note that when we pass a string as a list parameter, associated with the function split() the string string is divided into substrings and each of these substrings is transformed into elements and stored in the respective list. In this case, when we ask to print full sentence, we are shown a list in which, each element is a substring of the main string, ie substring of the phrase typed.

Another way to resolve this issue is by using List Comprehension. The code would look like this:

frase_inteira = [x for x in input('Digite a frase: ').split()]
print(frase_inteira)

Browser other questions tagged

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