Variable with multiple lists for array

1

Good evening. I have a text file where I have already converted your content to lists. Each line of the file has become a [] list, but now I need those lists all to be inside an array. This way I can not concatenate because there is only one variable saving all lists: Ex:

  

File contains:

     

1 'ola' 'ali' 393

     

2 'ola2' 'ali2' 394

     

3 'ola3' 'ali3' 395

Then I converted to lists, list = file.split ()

  

Return:

     

[1, 'ola', 'ali', 393)

     

[2, 'ola2', 'ali2', 394)

     

[3, 'ola3', 'ali3', 395]

So far so good, but now I need to throw all the lists that are inside the variable into an array, so I can scroll through lines and indexes and change as needed. It would have to stay:

  

array [[1, 'ola', 'ali', 393], [2, 'ola2', 'ali2', 394], [3, 'ola3', 'ali3', 395]]

I ended up doing it in a way that ended up getting each row an array, did not go as expected. The searches I've done always end up joining separate lists and separate variables. I imagine I need to do a line break or something, but I've already lost, I always run into the same examples.

Edit:

Look at the return of this code.

Edit18/10

Iwasabletocreatelistswithinthearray,butit'scolumnlistsandIcannotflip.HowcanIdotochangeeachcolumnbyanindex,getcolumn0ofallindexesandcreateanewlist,thencolumn1andnewlist,soIcanpickthechosenindexwiththecontentthatIwant.ItwouldbeanordinationbutIcannotdoit.Imageofthecodesofar:

    
asked by anonymous 16.10.2015 / 03:30

1 answer

1

In the question, you exposed the problem, but you did not present any code, so you could even know where you are, or what you have already tried.

Assuming I'm not sure how you opened the file, or what kind of file you're using, I'll show you an example I built, based on some of the details you provided, and I'll explain as briefly as possible. Probably the codes will not be identical, but there is no reason for alarms, it's easy to understand.

First open the file, and create a new array, then fill it with the data contained in the file:

#Abrir o ficheiro
arquivo = open('file.txt', 'r')
# Nova array
array = []

Then just convert each line contained in this file, into arrays:

#Criar novas arrays à partir do ficheiro
for i in arquivo:
    array.append(i.split())

And we get this as output:

  

[['1', '' '', '' '' '], [' 2 ',' '2' ',' '2' '], [' 3 ' , "'ali3'"]]

Finally, we just have to go through the positions of this new array:

#Percorrer a primeira camada
for y in array:
#Percorrer cada array
    for o, z in enumerate(y):
        print '(', z, ')', 'linha', o

In the first layer we get:

  

['1', '' ola '', '' ali '"]

     

['2', "'ola2'", "'ali2'"]

     

['3', "'ola3'", "'ali3'"]

The entire code looks like this:

arq = open('file.txt', 'r')
array = []

#Popular nova array a partir do ficheiro
for i in arquivo:
    array.append(i.split())

#Percorrer posicoes
for y in array:
    for o,z in enumerate(y):
        print '(', z, ')', 'linha:', o  

I wrote the code using python 2.7 .

    
16.10.2015 / 06:20