Showing posts with label List. Show all posts
Showing posts with label List. Show all posts

Nested List Sorting

# Nested List sort


L = [['IT', 56], ['CSE', 53], ['EEE', 52], ['ECE', 55]]


# Sorting a nested List using bubble sort

# default - sorts by first element of nested list


for i in range(0,len(L)):

for j in range(0,len(L)-1):

if(L[j]>L[j+1]):

L[j],L[j+1]=L[j+1],L[j]


print("Sort by first element (default)")

print(L)


# Sorting a nested List using bubble sort

# Sort by second element of nested list


for i in range(0,len(L)):

for j in range(0,len(L)-1):

if(L[j][1]>L[j+1][1]):

L[j],L[j+1]=L[j+1],L[j]

print("Sort by Second element")

print(L)


"""

Sample output

 >python NestedListSort.py


Sort by first element (default)


[['CSE', 53], ['ECE', 55], ['EEE', 52], ['IT', 56]]


Sort by Second element


[['EEE', 52], ['CSE', 53], ['ECE', 55], ['IT', 56]]


"""

PYTHON ASSIGNMENT 5: LISTS



Write a program to read a set of numbers from a user and store in a list. Eliminate duplicate elements.

Given a list of N numbers, read the element to search in the list. Display the number of occurrences of the element with its position.

Anagrams. Given a list of words display the sets of anagrams.

Given a list of integers, create a new list with cumulative sum; that is a new list where the ith element is the sum of the first i elements from the original list. For example, the cumulative sum of [4, 3, 6] is [4, 7, 13].

Create a nested list containing student details (Roll number, name and 3 marks). Print the roll number, name, total and average marks of each student. 

Given two lists that are sorted in ascending order, write a program to merge the lists into a single list that contains every item from both lists in sorted order.


Matrix

Addition, subtraction and multiplication of matrices.

Transpose of a matrix.

Sum of diagonal elements of a matrix.

Largest, smallest element in matrix.

Pascal triangle

# Pascal triangle - 5 lines

L = [[1],[1,1]]
lno = 2

while(lno < 5):
tmp = [1]
i = 1
while(i<lno):
ele = L[lno-1][i-1]+L[lno-1][i]
tmp.append(ele)
i+=1
tmp.append(1)
lno+=1
L.append(tmp)


for ele in L:
print(" "*lno, end=" ")
for e in ele:
print(e, end= " ")
lno-=1
print()

List - Matrix Multiplication

# MatrixMultiplication

Mat_A = []
Mat_B = []
Mat_Mul = []

print("Enter number of rows and columns :")
r1 = int(input("Matrix 1 - Row : "))
c1 = int(input("Matrix 1 - Column : "))
r2 = int(input("Matrix 2 - Row : "))
c2 = int(input("Matrix 2 - Column : "))

# Multiplication
if (c1 != r2):
    print("Rows and columns do not match.")
    print("Cannot multiply.")
else:
    print("Matrix A - Enter values : ")
    for i in range(0,r1,1):
        tmp = []
        for j in range(0,c1,1):
            ele = int(input())
            tmp.append(ele)
        Mat_A.append(tmp)
           
    print("Matrix B - Enter values : ")
    for i in range(0,r2,1):
        tmp = []
        for j in range(0,c2,1):
            ele = int(input())
            tmp.append(ele)
        Mat_B.append(tmp)
       
    for i in range(0,r1,1):
        tmp = []
        for j in range(0,c2,1):
            ele = 0
            for k in range(0,c1,1):
                ele = ele + (Mat_A[i][k] * Mat_B[k][j])
            tmp.append(ele)
        Mat_Mul.append(tmp)
       
    print("Product of given matrices : ")
   
    for row in Mat_Mul:
        for ele in row:
            print(ele, end=" ")
        print()

List - Matrix Subtraction

# Matrix Subtraction

Mat_A = []
Mat_B = []

Mat_Sub = []

print("Enter number of rows and columns :")
r1 = int(input("Matrix 1 - Row : "))
c1 = int(input("Matrix 1 - Column : "))
r2 = int(input("Matrix 2 - Row : "))
c2 = int(input("Matrix 2 - Column : "))
 
# Subtraction
if ( (r1 != r2) or (c1 != c2) ):
    print("Rows and columns of given matrices do not match.")
    print("Cannot Subtract the matrices.")
else:
    print("Matrix A - Enter values : ")
    for i in range(0,r1,1):
        tmp = []
        for j in range(0,c1,1):
            ele = int(input())
            tmp.append(ele)
        Mat_A.append(tmp)
           
    print("Matrix B - Enter values : ")
    for i in range(0,r2,1):
        tmp = []
        for j in range(0,c2,1):
            ele = int(input())
            tmp.append(ele)
        Mat_B.append(tmp)

    for i in range(0,r1,1):
        tmp = []
        for j in range(0,c1,1):
            ele = Mat_A[i][j] - Mat_B[i][j]
            tmp.append(ele)
        Mat_Sub.append(tmp)
   
    print("Difference of given matrices : ")
   
    for row in Mat_Sub:
        for ele in row:
            print(ele, end=" ")
        print()
  

List - Matrix Addition

# Matrix Addition

Mat_A = []
Mat_B = []
Mat_Add = []

print("Enter number of rows and columns :")
r1 = int(input("Matrix 1 - Row : "))
c1 = int(input("Matrix 1 - Column : "))
r2 = int(input("Matrix 2 - Row : "))
c2 = int(input("Matrix 2 - Column : "))

# Addition
if ( (r1 != r2) or (c1 != c2) ):
    print("Rows and columns of given matrices do not match.")
    print("Cannot Add the matrices.")
else:
    print("Matrix A - Enter values : ")
    for i in range(0,r1,1):
        tmp = []
        for j in range(0,c1,1):
            ele = int(input())
            tmp.append(ele)
        Mat_A.append(tmp)
           
    print("Matrix B - Enter values : ")
    for i in range(0,r2,1):
        tmp = []
        for j in range(0,c2,1):
            ele = int(input())
            tmp.append(ele)
        Mat_B.append(tmp)
   
    for i in range(0,r1,1):
        tmp = []
        for j in range(0,c1,1):
            ele = Mat_A[i][j] + Mat_B[i][j]
            tmp.append(ele)
        Mat_Add.append(tmp)

    print("Sum of given matrices : ")
   
    for row in Mat_Add:
        for ele in row:
            print(ele, end=" ")
        print()