Home >Backend Development >Python Tutorial >Python List Tutorial Day2
This Python code demonstrates several matrix and string manipulations. Let's break down each section:
1. Matrix Transpose:
This section calculates the transpose of a given matrix. The transpose of a matrix is obtained by interchanging its rows and columns. The code iterates through the rows and columns, building the transposed matrix. However, the while
loop is incomplete and contains a syntax error (cdefab
). Here's a corrected and more efficient version:
<code class="language-python">l = [[10, 12], [40, 2], [60, 3]] transpose = [[l[j][i] for j in range(len(l))] for i in range(len(l[0]))] print(transpose)</code>
This uses list comprehension for a concise and Pythonic solution.
2. String Rotation:
This part rotates a string by a specified number of positions. The num % len(word)
ensures that the rotation wraps around correctly, handling both positive and negative rotations. The code is functional.
3. Matrix Row/Column Operations:
This section performs several operations on a matrix: summing the elements of each row, finding the maximum and minimum values in each row. The code correctly calculates the sum of each row. However, the code for finding the minimum value is incomplete. Here's a corrected version:
<code class="language-python">student_marks = [[10, 20, 30], [40, 50, 60], [70, 80, 90]] # Row sums for marks_list in student_marks: row_sum = sum(marks_list) # Use the built-in sum() function print(f"Row Sum: {row_sum}") # Row maximums for marks_list in student_marks: row_max = max(marks_list) # Use the built-in max() function print(f"Row Max: {row_max}") # Row minimums for marks_list in student_marks: row_min = min(marks_list) # Use the built-in min() function print(f"Row Min: {row_min}") print("==============================================") </code>
This improved version leverages Python's built-in sum()
, max()
, and min()
functions for better readability and efficiency.
4. Matrix Column Sum and Leading Diagonal Sum:
This part is missing. To calculate the sum of columns and the leading diagonal, you would need to add the following code:
<code class="language-python"># Column sums column_sums = [sum(row[i] for row in student_marks) for i in range(len(student_marks[0]))] print(f"Column Sums: {column_sums}") # Leading diagonal sum (assuming a square matrix) leading_diagonal_sum = sum(student_marks[i][i] for i in range(len(student_marks))) print(f"Leading Diagonal Sum: {leading_diagonal_sum}")</code>
This code efficiently calculates column sums using list comprehension and the leading diagonal sum. Remember that the leading diagonal sum only works correctly for square matrices (matrices with the same number of rows and columns).
In summary, the original code has some errors and omissions. The provided corrections and additions offer a more complete and efficient implementation of the intended matrix and string manipulations. Using built-in functions whenever possible significantly improves code readability and performance.
The above is the detailed content of Python List Tutorial Day2. For more information, please follow other related articles on the PHP Chinese website!