Variable with multiple lists for matrix

Asked

Viewed 1,141 times

1

Good night. I have a text file where I have already converted your content to lists. Each line in the file has become a list[], but now I need these lists to be all within an array. This way I can’t concatenate because there is only one variable holding all the lists: Ex:

File contains:

1 'ola' 'ali' 393

2 'ola2' 'ali2' 394

3 'ola3' 'ali3' 395

Then 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 play all the lists that are within the variable for an array, so I can scroll through lines and indexes and change as necessary. Would have to stay:

matrix [[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 leave as expected. The research I’ve done always ends up putting together separate lists and separate variables. I imagine I need to do a line break or something, but I’ve already lost myself, I keep bumping into the same examples.

Edit:

Look at the return of this code.

Código

Edit 18/10

I was able to create lists inside the matrix, but it’s column lists and I can’t invert. How can I exchange each column for an index, I take column 0 of all indexes and create a new list, then column 1 and new list, so I can get the chosen index with the content I want. It would be an ordination, but I can’t seem to do it. Image of the code so far:

Código

  • Could you post your code showing what you’ve tried? So it’s better for the community to help you.

  • Look I honestly did not understand what you did, here is not returning any break, prints the total value of each position, maybe it was the n that was misplaced, try the code again, this time simply try to replace the file name.

1 answer

1

In the question, you exposed the problem, but you didn’t present any code, so at least I could know where you are, or what you’ve experienced.

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, according to some of the details you provided and I’ll explain as briefly as possible. The codes probably won’t be identical, but there’s no reason for alarms, it’s easy to understand.

The file is first opened, and a new array is created and then filled in 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 that file into arrays:

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

And we get this as a way out:

[['1', "'ola'", "'ali'"], ['2', "'ola2'", "'ali2'"], ['3', "'ola3'", "'ali3''"]]

Finally, just 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 whole code goes 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.

  • Did you have something different to use in this your python code 2.7 to 3.x, because in my case returns different, posted a photo.

  • Check this out: http://imgur.com/a/7tvDy

  • I had set zero instead of 'o', but it’s not quite what I need this enumeration, I can’t access it individually. I did a test with your code collecting the data of the array, it adds a new list to each row, but in case I need the result of the last line where it contains all the lists, I will post two photos. This is the result I want https://copy.com/E8LTnlxJ2FkmgqVV and this is the return of your code without the https://copy.com/l7QYxbb1sE7ymfGM enumeration. I’m sorry if I don’t understand, beginner knows how it is.

  • Edilson, I almost got but missing a detail, an ordination to be able to access as wish, I attached a photo to the post.

  • I’d forgotten until, I’ll see what I can do.

Browser other questions tagged

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