0% found this document useful (0 votes)
25 views19 pages

ComprehensionsList Dictionary

This document contains code snippets demonstrating various list, dictionary, and matrix operations in Python including: 1) List comprehensions to create lists from ranges and nested lists. 2) Dictionary comprehensions to create dictionaries from character codes. 3) Different methods for transposing a matrix including nested for loops, while loops, and list comprehensions. 4) Representing a sparse matrix using a compressed list format. 5) Techniques for cloning lists including slicing, copy, and using the list constructor.

Uploaded by

Niraj Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views19 pages

ComprehensionsList Dictionary

This document contains code snippets demonstrating various list, dictionary, and matrix operations in Python including: 1) List comprehensions to create lists from ranges and nested lists. 2) Dictionary comprehensions to create dictionaries from character codes. 3) Different methods for transposing a matrix including nested for loops, while loops, and list comprehensions. 4) Representing a sparse matrix using a compressed list format. 5) Techniques for cloning lists including slicing, copy, and using the list constructor.

Uploaded by

Niraj Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Comprehensions(List,Dictionary)

Sparse matrices, aliasing and copying


L14,L16 and L17
#Program illustrating simple list comprehension
data = input("str: ")
l1 = [i for i in data]
print(l1)
#Program to illustrate Nested List Comprehension
L1 = [a for a in range(50) if a % 2 == 0 if a % 3 == 0]
print(L1)
#Program to transpose a matrix in different waysprint("transposition using nested for:")
matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] """ Matrix Transposition using Nested Fors"""
transposed = [] # Transposed Matrix
transposed = []
for i in range(len(matrix[0])):
print("matrix:") transposed_row = []
print(matrix) for row in matrix: # Build each row
print("transposition using nested while:") transposed_row.append(row[i])
""" Matrix Transposition using Nested while""" transposed.append(transposed_row) # Append the row
print(transposed)
i=0
print("transposition single list comprehension:")
while(i < len(matrix[0])): """ Matrix Transposition using single list comprehension"""
j=0 transposed = []
lx = [] #list comprehension used to build the row
while(j < len(matrix)): for i in range(len(matrix[0])):
transposed.append([row[i]for row in matrix])
lx.append(matrix[j][i])
print(transposed)
j=j+1 print("transposition double list comprehension:")
transposed.append(lx) """ Matrix Transposition using double list comprehension"""
i=i+1 #list comprehension used to build the row and column
print(transposed) trasposed = []
transposed = [[row[i]for row in matrix]for i in range(len(matrix[0]))]
print(transposed)
list1 = [int(x) for x in input("data: ").split()]
print("contents:", list1)
#Program to illustrate Dict comprehension
ordchar = {x:chr(x) for x in range(65, 91)}
charord = {x:y for y, x in ordchar.items()}
print(sorted(ordchar.items()))
print(sorted(charord.items()))
k=0
# Sparse Matrix Representation using lists for i in range(m):
for j in range(n):
if (sparseMatrix[i][j] != 0):
compressMatrix[0][k] = i
def sparseMatrix(sparseMatrix, m, n): compressMatrix[1][k] = j
# initialize size as 0 compressMatrix[2][k] = sparseMatrix[i][j]
size = 0 k += 1

for i in range(m): print("Sparse representation:")


for j in range(n): for i in compressMatrix:
if (sparseMatrix[i][j] != 0): print(i)

size += 1 m = int(input("Enter row size: "))


n = int(input("Enter col size: "))
# number of columns in compressMatrix(size) # print("Enter elements:")
should be equal to number of # non-zero elementsmat = [[int(input()) for x in range (n)] for y in range(m)]
in sparseMatrix print("Sparse matrix is:")
for i in range(m):
rows, cols = (3, size) for j in range(n):
compressMatrix = [[0 for i in range(cols)] for j in print(mat[i][j],end = " ")
range(rows)] print()
sparseMatrix(mat, m, n)
#cloning using slicing #cloning using copy method

a = [1, 2, 3, 4, 5] a = [1, 2, 3, 4, 5]

print("a =", a) print("a =", a)

print("b = a[:]") print("b = a.copy()")

b = a[:] b = a.copy()

print("b =", b) print("b =", b)

print("a is b ? :", a is b) print("a is b ? :", a is b)

#cloning using list function


a = [1, 2, 3, 4, 5]
print("b = list(a)")
b = list(a)
print("b =", b)
print("a is b ? :", a is b)
print("a[0] = 100 ")
a[0] = 100
print("a =", a)
print("b =", b)

You might also like