简体   繁体   中英

How do you manipulate nested lists in Python?

I have a Matrix like the next one, but bigger:

m = [[38, 38,  0],
     [39, 38,  0],
     [40, 38,  0],
     [41, 38,  3],
     [42, 38,  0],
     [43, 38,  4],
     [44, 38,  4],
     [45, 38,  5],
     [38, 39,  0],
     [39, 39,  0],
     [40, 39,  0],
     [41, 39,  3],
     [42, 39,  0],
     [43, 39,  4],
     [44, 39,  4],
     [45, 39,  5],
     [38, 40,  0],
     [39, 40,  0],
     [40, 40,  0],
     [41, 40,  3],
     [42, 40,  0],
     [43, 40,  4],
     [44, 40,  4],
     [45, 40,  5]]

And I would like to change the values of the first two columns in a loop to have make them start in 1.

The result would be the following Matrix:

[[1, 1,  0],
 [2, 1,  0],
 [3, 1,  0],
 [4, 1,  3],
 [5, 1,  0],
 [6, 1,  4],
 [7, 1,  4],
 [8, 1,  5],
 [1, 2,  0],
 [2, 2,  0],
 [3, 2,  0],
 [4, 2,  3],
 [5, 2,  0],
 [6, 2,  4],
 [7, 2,  4],
 [8, 2,  5],
 [1, 3,  0],
 [2, 3,  0],
 [3, 3,  0],
 [4, 3,  3],
 [5, 3,  0],
 [6, 3,  4],
 [7, 3,  4],
 [8, 3,  5]]

I'd appreciate a detailed explanation because I am new in python. :)

Assumptions

I will answer below, but a good practise on StackOverflow is to show what you already tried, and where you got stuck. Also, Python has two major versions at the moment: 2.7 and 3. I don't know which one you're using, so I'll be using examples from Python 3. The major versions do not differ drastically, so there's a good chance the examples will work in both versions.

As @MK Patel stated in a comment, we are not sure if your matrix has three elements for each row, or just one (malformatted). I am going to assume that you mean [1, 1, 0] instead of [1. 1. 0.] [1. 1. 0.] .

Answer & Examples

To start, let's say your matrix is in m: m =[[38, 38, 0.], ...]

The easiest way to loop over all your rows, is like so:

for row in m:
    # do stuff with row
    print(row[1]) # print second column

The problem with this approach is that you have to keep count of what number of row you are on yourself (for example, each loop you can increment a variable "rowCount").

To make Python do the work for you here, you can iterate an integer over the length of your matrix. This is done like so:

for i in range(len(m)):
    # your row is now in m[i]
    print(m[i][1]) # print second column

Here, len(m) equals to the length of your array. range(n) is shorthand for a list containing [0, 1..., n]. This way, you can set your rows' first column (index 0) to match the row count, so this column starts at 1, and goes up together with your loop. Keep in mind that Python (and most other programming languages) will start counting at 0, so you will have to add 1 each time.

Since you want to "restart" your count for every new value in the second column, you can use the modulo ( % ) operator to make i restart at 1 every time it goes up by 8. This is based on an assumption I made about your second column. Read more in the next paragraph.

for i in range(len(m)):
    # set first column to i + 1, to start counting from 1 and go up every time
    m[i][0] = (i % 8) + 1

For your second column, I am also missing some information. There are a few approaches to your problem, depending on what is always true. The most noticable thing is that every number repeats 8 times, so I will use this fact in the code.

We can use the integer devision operator ( // ) to add one for every 8 that our iterator ( i ) goes up.

for i in range(len(m)):
    # set first column to i + 1, to start counting from 1 and go up every time
    m[i][0] = (i % 8) + 1
    # set the second column to (i // 8) + 1, going up one every 8 rows (and start at one)
    m[i][1] = (i // 8) + 1

I hope this helped.

You can solve it in one line of code in python... This method call as comprehension method... Your solution is...

final_matrix = [[inner_matrix[0]-37, inner_matrix[1]-37, inner_matrix[2]] for inner_matrix in m]

print(final_matrix)

and if you are new in python then you can solve it as other language like...

result= []
for inner_matrix in m:
    inner_matrix[0] -= 37
    inner_matrix[1] -= 37
    result.append(inner_matrix)

print(result)

Note:- Above solution is not a good practice for python..this is only for your easy understanding...in real you should solve it using comprehension method.

If you dont know about comprehension method then.... Read topic 5.1.3

If you know the first cell contains the minimal value, a simple way is to iterate over all the rows and modify the first X columns by subtracting the minimal value.

In other words, in your example, the minimal value is 38, so you just need to subtract 37 to the first two columns.

Let's say your matrix is m , the complete code is:

# get the value that will be translated to 1
# the - 1 is here to start at 1, not 0
start_val = m[0][0] - 1

# iterate over all the rows
for row in m:
    # iterate over the first 2 columns
    for i in range(2): # i = 0, i = 1
        # change the value
        row[i] -= start_val 

Note that in this snippet, we modify the matrix "inplace", ie directly.


Just for fun, this could also be done using this one-liner (see the list comprehension concept in functional python programming):

start_val = m[0][0] - 1
new_m = [[c-start_val for c in row[:2]] + row[2:] for row in m]

A more general approach would be to use Python list comprehension.

You can do what you want to each of the columns independently. The following example does exactly what you want...

new_matrix = [[i[0]-37, i[1]-37, i[2]] for i in a]

Where the number 37 can be determined from the initial list if you don't want to hard code it.

Here is a good tutorial on how list comprehension works in Python.

You can try this simple one:

my_list = [[38, 38, 0, ],
           [39, 38, 0, ],
           [40, 38, 0, ],
           [41, 38, 3, ],
           [42, 38, 0, ],
           [43, 38, 4, ],
           [44, 38, 4, ],
           [45, 38, 5, ],
           [38, 39, 0, ],
           [39, 39, 0, ],
           [40, 39, 0, ],
           [41, 39, 3, ],
           [42, 39, 0, ],
           [43, 39, 4, ],
           [44, 39, 4, ],
           [45, 39, 5, ],
           [38, 40, 0, ],
           [39, 40, 0, ],
           [40, 40, 0, ],
           [41, 40, 3, ],
           [42, 40, 0, ],
           [43, 40, 4, ],
           [44, 40, 4, ],
           [45, 40, 5, ], ]

first_column = 1
second_column = 1
for i in range(len(my_list)):
    if first_column > 8: #if value of first column is greater than 8 then reset first_column and second_column
        first_column = 1
        second_column = second_column + 1
    my_list[i][0] = first_column
    my_list[i][1] = second_column
    first_column = first_column + 1

print(my_list)
  • Iterate through the list
  • Change the value of the first and second column
  • If value of first_column is greater than 8 then increment value of second_column and make first_column=1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM