[Python Programming Basics to Advanced]: Lambda Functions and Sorting Data | Lab 29

preview_player
Показать описание
This Python programming playlist is designed to take beginners with zero programming experience to an expert level. The course covers installation, basic syntax, practical scenarios, and efficient logic building. The course material includes PDF handouts, review questions, and covers a wide range of topics, from data types to advanced functions like Lambda and Recursive functions, Generators, and JSON data parsing.

In this lesson we will study about Lambda Functions also known as Anonymous Functions. These are the functions with no names. These are also called as Lambda Expression.
Then we will see the sorting of different data types and will see how we can use the lambda function as key of sorting. We will see multi-level sorting and parallel sorting and will use the concepts on complex dataset.

Link of Movies Task file:

Link of Student Data Task file:

The csv file used in Student Task:

Complete Playlist:

If you have the basic programming knowledge and interested to learn Object-Oriented Programming in Python, check out this playlist:

Lab Manual 29 can be downloaded from here:

Review Question:
1- At 10:10 we saw that we cannot sort a nested list having inner list and tuples together. Now use the argument key efficiently so that inner lists and tuples are sorted on the basis of first element.

2- We have a list of XY points as shown here: p=[(2,1),(3,1),(1,5),(1,0),(4,2)]
Sort the list of points based on their distance from point (5,5)

3- Do review question given at 32:02

4- Here is a list of few numbers: [2,5,-1,4,-2,1,0]
First generate a sorted list based on the magnitude of the number.
Then generate another sorted list in reverse order (from original list) based on magnitude, and if the magnitude is same then positive number should come before negative number.

#PythonProgramming #python #pythontutorial
Рекомендации по теме
Комментарии
Автор

At 15:50 I said that there is key optional argument for the max() function as well. It means that Python interpreter will apply the function specified as the key on all elements of the iterable, and the element giving the maximum output on that function will be the output.

LearningOrbis
Автор

from math import sqrt
#Q----1
original_list=([2, 3, 1], (5, 3, 2, 1, 5), [2, 4])
sorted_list=sorted(original_list, key=lambda e: tuple(e))
print(sorted_list)


points=[(2, 1), (3, 1), (1, 5), (1, 0), (4, 2)]
y=sorted(points, key=lambda n: sqrt((5-n[0] )**2 + (5-n[1])**2) ) #output are points sorted on the basis of the distance from point (5, 5)
print(f'Required Output: {y}')

num_list=[2, 5, -1, 4, -2, 1, 0]
s=sorted(num_list, key=abs) #sort on the basis of magnitude.
t=sorted(num_list, reverse=True, key=lambda x:(abs(x), x>0) ) #First we sort on the basis of magnitude then on the basis of +vity of a number.
print(s)
print(t)

abdurrehmansarwar
Автор

AOA sir, one thing is missed here. SIr i do not know others, but i have no access to download csv student file.That's why, i cannot solve question related to csv file

hafizasadullah
Автор

# QUESTION 1
a=[[1, 3, 4, 5], [2.34, 5], (3, 4, 3, 2)]
b=sorted(a, key=lambda m:m[0])
print(b)

# QUESTION 2
from math import sqrt
p = [(2, 1), (3, 1), (1, 5), (1, 0), (4, 2)]
e=(5, 5)
c=sorted(p, key=lambda m:sqrt(e[0]*2-m[0]*2))
print(c)

# QUESTION 3

a=[2, 5, -1, 4, -2, 1, 0]
b=sorted(a)
a.sort(reverse=True)
print(a)
print(b)

hafizasadullah
Автор

Question # 1:
x=[(2, 4, 1), [5, 3, 2, 1, 5], (2, 3), [4, 9, 2]]
print(sorted(x, key=list))

Question # 2:

from math import sqrt
p=[(2, 1), (3, 1), (1, 5), (1, 0), (4, 2)]
print(sorted(p, key=lambda p: sqrt((5-p[0])**2 + (5-p[1])**2) ))


Question # 3:
import csv

courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
with open('studentsData.csv', 'r') as f:
r=csv.DictReader(f)
data=[]
d={}
for rec in r:
d={}
for k, v in rec.items():
if(k in ['Reg', 'Name', 'Sec']):
d[k]=v
if (k in courses and v!='NR'):
if ('Courses' not in d):
d['Courses']={k:v}
else:
d['Courses'][k]=v
data.append(d)

courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
CH=[3, 2, 3, 3, 2]
subjects=dict(zip(courses, CH))

gLetter=['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'F']
gPoints=[4.0, 4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.7, 1.3, 1.0, 0]
gDict=dict(zip(gLetter, gPoints))

def calculateGPA(a):
l=[subjects[i]*gDict[j] for i, j in a['Courses'].items()]
return sum(l)/sum(CH)

a=sorted(data, key=calculateGPA)
print([i['Reg']for i in a])


Question # 4:
d=[2, 5, -1, 4, -2, 1, 0]
print(sorted(d, key=abs))
print(sorted(d, key=lambda d:(abs(d), d>0), reverse=True)

shahzaibaliafaq
Автор

#ReviewQuestion1:
x=[(2, 4, 1), [5, 3, 2, 1, 5], (2, 3), [4, 9, 2]]
print(sorted(x, key=list))

#ReviewQuestion2:
from math import sqrt
p=[(2, 1), (3, 1), (1, 5), (1, 0), (4, 2)]
print(sorted(p, key=lambda p: sqrt((5-p[0])**2 + (5-p[1])**2) ))

#ReviewQuestion3:
import csv
courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
with open('studentsData.csv', 'r') as f:
r=csv.DictReader(f)
data=[]
d={}
for rec in r:
d={}
for k, v in rec.items():
if(k in ['Reg', 'Name', 'Sec']):
d[k]=v
if (k in courses and v!='NR'):
if ('Courses' not in d):
d['Courses']={k:v}
else:
d['Courses'][k]=v
data.append(d)

courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
CH=[3, 2, 3, 3, 2]
subjects=dict(zip(courses, CH))

gLetter=['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'F']
gPoints=[4.0, 4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.7, 1.3, 1.0, 0]
gDict=dict(zip(gLetter, gPoints))

def calculateGPA(a):
l=[subjects[i]*gDict[j] for i, j in a['Courses'].items()]
return sum(l)/sum(CH)

a=sorted(data, key=calculateGPA)
print([i['Reg']for i in a])

#ReviewQuestion4:
d=[2, 5, -1, 4, -2, 1, 0]
print(sorted(d, key=abs))
print(sorted(d, key=lambda d:(abs(d), d>0), reverse=True))

usman_tariq
Автор

Question 01
x=[(2, 4, 1), [5, 3, 2, 1, 5], (2, 3), [4, 9, 2]]
print(sorted(x, key=list))

Question 02

from math import sqrt
p=[(2, 1), (3, 1), (1, 5), (1, 0), (4, 2)]
print(sorted(p, key=lambda p: sqrt((5-p[0])**2 + (5-p[1])**2) ))


Question 03
import csv

courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
with open('studentsData.csv', 'r') as f:
r=csv.DictReader(f)
data=[]
d={}
for rec in r:
d={}
for k, v in rec.items():
if(k in ['Reg', 'Name', 'Sec']):
d[k]=v
if (k in courses and v!='NR'):
if ('Courses' not in d):
d['Courses']={k:v}
else:
d['Courses'][k]=v
data.append(d)

courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
CH=[3, 2, 3, 3, 2]
subjects=dict(zip(courses, CH))

gLetter=['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'F']
gPoints=[4.0, 4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.7, 1.3, 1.0, 0]
gDict=dict(zip(gLetter, gPoints))

def calculateGPA(a):
l=[subjects[i]*gDict[j] for i, j in a['Courses'].items()]
return sum(l)/sum(CH)

a=sorted(data, key=calculateGPA)
print([i['Reg']for i in a])


Question 04
d=[2, 5, -1, 4, -2, 1, 0]
print(sorted(d, key=abs))
print(sorted(d, key=lambda d:(abs(d), d>0), reverse=True)

zohaibkhalid
Автор

I have xml and i can sort within first child only. but it will through an error when the line have commented in my 1st child. it works fine if there is NO comments. can you help me here
CODE
root = tree.getroot()

for c in root:
c[:] = sorted(c, key=lambda child: (child.tag, child.get('name')))

xmlstr = ET.tostring(root, encoding="utf-8", method="xml")
print(xmlstr.decode("utf-8"))

ERROR
c[:] = sorted(c, key=lambda child: (child.tag, child.get('name')))
TypeError: '<' not supported between instances of 'function' and 'str'

malteshjoshi
Автор

Question No.1:-

a=([2, 3, 1], (5, 3, 2, 1, 5), [2, 4])
print(sorted(a, key=lambda x:x[0]))

Question No.2:-

from math import *
p=[(2, 1), (3, 1), (1, 5), (1, 0), (4, 2)]
print(sorted(p, key=lambda


Question No.4:-

lis1=[2, 5, -1, 4, -2, 1, 0]

print(sorted(lis1, key=lambda x:abs(x)))
print(sorted(lis1, key=lambda x:(abs(x), x>0), reverse=True, ))

MuhammadAhmad-dshu
Автор

Task-1

x=[(2, 4, 1), [5, 3, 2, 1, 5], (2, 3), [4, 9, 2]]

print(sorted(x, key=list))

Task-2

from math import sqrt

p=[(2, 1), (3, 1), (1, 5), (1, 0), (4, 2)]

print(sorted(p, key=lambda

Task-3

def GPA(a):

for i in subjects if i in a['Courses']]

ch=[subjects[i] for i in subjects if i in a['Courses']]

return sum(gp)/sum(ch)


Main PROGRAM

courses=['DLD', 'CP', 'ED', 'VCA', 'EM']

CH=[3, 2, 3, 3, 2]

subjects=dict(zip(courses, CH))

gLetter=['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'F']

gPoints=[4.0, 4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.7, 1.3, 1.0, 0]

gDict=dict(zip(gLetter, gPoints))



a=print(sorted(data, key=GPA))

print([i['Reg']for i in a])

Task-4

a=[2, 5, -1, 4, -2, 1, 0]

print(sorted(a, key=abs))

print(sorted(a, key=lambda b:(abs(b), b>0), reverse=True))

wasifalii
Автор

#REVIEW Q1:
x=[(2, 4, 1), [5, 3, 2, 1, 5], (2, 3), [4, 9, 2]]
print(sorted(x, key=list))

#REVIEW Q2:
from math import sqrt
p=[(2, 1), (3, 1), (1, 5), (1, 0), (4, 2)]
print(sorted(p, key=lambda p: sqrt((5-p[0])**2 + (5-p[1])**2) ))

#REVIEW Q3:
import csv

courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
with open('studentsData.csv', 'r') as f:
r=csv.DictReader(f)
data=[]
d={}
for rec in r:
d={}
for k, v in rec.items():
if(k in ['Reg', 'Name', 'Sec']):
d[k]=v
if (k in courses and v!='NR'):
if ('Courses' not in d):
d['Courses']={k:v}
else:
d['Courses'][k]=v
data.append(d)

courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
CH=[3, 2, 3, 3, 2]
subjects=dict(zip(courses, CH))

gLetter=['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'F']
gPoints=[4.0, 4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.7, 1.3, 1.0, 0]
gDict=dict(zip(gLetter, gPoints))

def calculateGPA(a):
l=[subjects[i]*gDict[j] for i, j in a['Courses'].items()]
return sum(l)/sum(CH)

a=sorted(data, key=calculateGPA)
print([i['Reg']for i in a])

#REVIEW Q4:
d=[2, 5, -1, 4, -2, 1, 0]
print(sorted(d, key=abs))
print(sorted(d, key=lambda d:(abs(d), d>0), reverse=True))

AsadAli-owie
Автор

2019-MC-17


Question 01
x=[(2, 4, 1), [5, 3, 2, 1, 5], (2, 3), [4, 9, 2]]
print(sorted(x, key=list))

Question 02

from math import sqrt
p=[(2, 1), (3, 1), (1, 5), (1, 0), (4, 2)]
print(sorted(p, key=lambda p: sqrt((5-p[0])**2 + (5-p[1])**2) ))


Question 03
import csv

courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
with open('studentsData.csv', 'r') as f:
r=csv.DictReader(f)
data=[]
d={}
for rec in r:
d={}
for k, v in rec.items():
if(k in ['Reg', 'Name', 'Sec']):
d[k]=v
if (k in courses and v!='NR'):
if ('Courses' not in d):
d['Courses']={k:v}
else:
d['Courses'][k]=v
data.append(d)

courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
CH=[3, 2, 3, 3, 2]
subjects=dict(zip(courses, CH))

gLetter=['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'F']
gPoints=[4.0, 4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.7, 1.3, 1.0, 0]
gDict=dict(zip(gLetter, gPoints))

def calculateGPA(a):
l=[subjects[i]*gDict[j] for i, j in a['Courses'].items()]
return sum(l)/sum(CH)

a=sorted(data, key=calculateGPA)
print([i['Reg']for i in a])


Question 04
d=[2, 5, -1, 4, -2, 1, 0]
print(sorted(d, key=abs))
print(sorted(d, key=lambda d:(abs(d), d>0), reverse=True))

MuhammadHassan-ldyt
Автор

# from math import sqrt
# # REVIEW QUESTION 1:
# a=([2, 3, 1], (5, 3, 2, 1, 5), [2, 4])
# print(sorted(a, key=list))

# REVIEW QUESTION 2
# pts=[(2, 1), (3, 1), (1, 5), (1, 0), (4, 2)]
# a=(5, 5)
# print(sorted(pts, key=lambda

# REVIEW QUESTION 3
import csv
courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
with open('studentsData.csv', 'r') as f:
r=csv.DictReader(f)
data=[]
d={}
for rec in r:
d={}
for k, v in rec.items():
if(k in ['Reg', 'Name', 'Sec']):
d[k]=v
if (k in courses and v!='NR'):
if ('Courses' not in d):
d['Courses']={k:v}
else:
d['Courses'][k]=v
data.append(d)
def GPA(x):
s=[sub[i]*gDict[j] for i, j in x['Courses'].items()]
return sum(s)/sum(ch)
courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
ch=[3, 2, 3, 3, 2]
sub=dict(zip(courses, ch))
gLetter=['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'F']
gPoints=[4.0, 4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.7, 1.3, 1.0, 0]
gDict=dict(zip(gLetter, gPoints))
a=sorted(data, key=GPA)
print([i['Reg']for i in a])

# REVIEW QUESTION 4
list2=[2, 5, -1, 4, -2, 1, 0]
print(sorted(list2, key=lambda x:abs(x)))
print(sorted(list2, key=lambda x:(abs(x), x), reverse=True))

MuhammadQasim-kwer
Автор

#Review Question 1
x=[(2, 4, 1), [5, 3, 2, 1, 5], (2, 3), [4, 9, 2]]
print(sorted(x, key=list))

#Review Question 2

from math import sqrt
p=[(2, 1), (3, 1), (1, 5), (1, 0), (4, 2)]
print(sorted(p, key=lambda p: sqrt((5-p[0])**2 + (5-p[1])**2) ))



#Review Question 3
import csv

courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
with open('studentsData.csv', 'r') as f:
r=csv.DictReader(f)
data=[]
d={}
for rec in r:
d={}
for k, v in rec.items():
if(k in ['Reg', 'Name', 'Sec']):
d[k]=v
if (k in courses and v!='NR'):
if ('Courses' not in d):
d['Courses']={k:v}
else:
d['Courses'][k]=v
data.append(d)

courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
CH=[3, 2, 3, 3, 2]
subjects=dict(zip(courses, CH))

gLetter=['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'F']
gPoints=[4.0, 4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.7, 1.3, 1.0, 0]
gDict=dict(zip(gLetter, gPoints))

def calculateGPA(a):
l=[subjects[i]*gDict[j] for i, j in a['Courses'].items()]
return sum(l)/sum(CH)

a=sorted(data, key=calculateGPA)
print([i['Reg']for i in a])




#Review Question 4
d=[2, 5, -1, 4, -2, 1, 0]
print(sorted(d, key=abs))
print(sorted(d, key=lambda d:(abs(d), d>0), reverse=True))

zainulhassan
Автор

#task 1
x=[(2, 4, 1), [5, 3, 2, 1, 5], (2, 3), [4, 9, 2]]
print(sorted(x, key=list))

#Task-2
from math import sqrt
p=[(2, 1), (3, 1), (1, 5), (1, 0), (4, 2)]
print(sorted(p, key=lambda

#TAsk-3
def GPA(a):
for i in subjects if i in a['Courses']]
ch=[subjects[i] for i in subjects if i in a['Courses']]
return sum(gp)/sum(ch)

#MAIN PROGRAM#
courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
CH=[3, 2, 3, 3, 2]
subjects=dict(zip(courses, CH))
gLetter=['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'F']
gPoints=[4.0, 4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.7, 1.3, 1.0, 0]
gDict=dict(zip(gLetter, gPoints))

a=print(sorted(data, key=GPA))
print([i['Reg']for i in a])

#TAsk-4
a=[2, 5, -1, 4, -2, 1, 0]
print(sorted(a, key=abs))
print(sorted(a, key=lambda b:(abs(b), b>0), reverse=True))

noorulhuda
Автор

#Q-1


a=([2, 3, 1], (5, 3, 2, 1, 50), [2, 4])
print(sorted(a, key=list))




#Q-2


from math import *
p=[(2, 1), (3, 1), (1, 5), (1, 0), (4, 2)]
print(sorted(p, key=lambda




#Q-3



def GPA(b):
y=[subjects[k]*gDict[v] for k, v in b['courses'].items()]
return sum(y)/sum(CH)



#MAIN PROGRAM

courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
CH=[3, 2, 3, 3, 2]
subjects=dict(zip(courses, CH))


gLetter=['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'F']
gPoints=[4.0, 4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.7, 1.3, 1.0, 0]
gDict=dict(zip(gLetter, gPoints))


x=sorted(data, key=GPA)
print([i['Reg']for i in x ])



#Q-4


a=[2, 5, -1, 4, -2, 1, 0]
print(sorted(a, key=abs))
print(sorted(a, key=lambda x:(abs(x), x>0), reverse=True))

rajahaseebahmad
Автор

#REVIEW QUESTON 1
g=[[2, 3, 1], (5, 3, 2, 1, 5), [2, 4]]
print(sorted(g, key= lambda a:list(a)))


#REVIEW QUESTON 2
from math import sqrt
def dist(t):
x, y=t
return sqrt((5-x)**2+(5-y)**2)
#MAIN PROGRAM
p=[(2, 1), (3, 1), (1, 5), (1, 0), (4, 2)]
print(sorted(p, key=dist))

#REVIEW QUESTON 3
import csv
courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
with open('studentdata.csv', 'r') as f:
r=csv.DictReader(f)
data=[]
d={}
for rec in r:
d={}
for k, v in rec.items():
if(k in ['Reg', 'Name', 'Sec']):
d[k]=v
if (k in courses and v!='NR'):
if ('Courses' not in d):
d['Courses']={k:v}
else:
d['Courses'][k]=v
data.append(d)
def calculateGPA(a):
l=[sub[i]*gmap[j] for i, j in a['Courses'].items()]
return sum(l)/sum(CH)

courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
CH=[3, 2, 3, 3, 2]
sub=dict(zip(courses, CH))

gLetter=['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'F']
gPoints=[4.0, 4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.7, 1.3, 1.0, 0]
gmap=dict(zip(gLetter, gPoints))

a=sorted(data, key=calculateGPA)
print([i['Reg']for i in a])


#REVIEW QUESTON 4
Z=[2, 5, -1, 4, -2, 1, 0]
print(sorted(Z, key=abs))
print(sorted(Z, key=lambda a:(abs(a), a>0), reverse=True))

huzaifasarwar
Автор

Question # 1:
x=[(2, 4, 1), [5, 3, 2, 1, 5], (2, 3), [4, 9, 2]]
print(sorted(x, key=list))

Question # 2:

from math import sqrt
p=[(2, 1), (3, 1), (1, 5), (1, 0), (4, 2)]
print(sorted(p, key=lambda p: sqrt((5-p[0])**2 + (5-p[1])**2) ))


Question # 3:
import csv

courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
with open('studentsData.csv', 'r') as f:
r=csv.DictReader(f)
data=[]
d={}
for rec in r:
d={}
for k, v in rec.items():
if(k in ['Reg', 'Name', 'Sec']):
d[k]=v
if (k in courses and v!='NR'):
if ('Courses' not in d):
d['Courses']={k:v}
else:
d['Courses'][k]=v
data.append(d)

courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
CH=[3, 2, 3, 3, 2]
subjects=dict(zip(courses, CH))

gLetter=['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'F']
gPoints=[4.0, 4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.7, 1.3, 1.0, 0]
gDict=dict(zip(gLetter, gPoints))

def calculateGPA(a):
l=[subjects[i]*gDict[j] for i, j in a['Courses'].items()]
return sum(l)/sum(CH)

a=sorted(data, key=calculateGPA)
print([i['Reg']for i in a])


Question # 4:
d=[2, 5, -1, 4, -2, 1, 0]
print(sorted(d, key=abs))
print(sorted(d, key=lambda d:(abs(d), d>0), reverse=True)

zoniseithzoniseith
Автор

Question 01
x=[(2, 4, 1), [5, 3, 2, 1, 5], (2, 3), [4, 9, 2]]
print(sorted(x, key=list))

Question 02

from math import sqrt
p=[(2, 1), (3, 1), (1, 5), (1, 0), (4, 2)]
print(sorted(p, key=lambda p: sqrt((5-p[0])**2 + (5-p[1])**2) ))


Question 03
import csv

courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
with open('studentsData.csv', 'r') as f:
r=csv.DictReader(f)
data=[]
d={}
for rec in r:
d={}
for k, v in rec.items():
if(k in ['Reg', 'Name', 'Sec']):
d[k]=v
if (k in courses and v!='NR'):
if ('Courses' not in d):
d['Courses']={k:v}
else:
d['Courses'][k]=v
data.append(d)

courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
CH=[3, 2, 3, 3, 2]
subjects=dict(zip(courses, CH))

gLetter=['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'F']
gPoints=[4.0, 4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.7, 1.3, 1.0, 0]
gDict=dict(zip(gLetter, gPoints))

def calculateGPA(a):
l=[subjects[i]*gDict[j] for i, j in a['Courses'].items()]
return sum(l)/sum(CH)

a=sorted(data, key=calculateGPA)
print([i['Reg']for i in a])


Question 04
d=[2, 5, -1, 4, -2, 1, 0]
print(sorted(d, key=abs))
print(sorted(d, key=lambda d:(abs(d), d>0), reverse=True)

MuhammadTalha-xmul
Автор

Question 01
x=[(2, 4, 1), [5, 3, 2, 1, 5], (2, 3), [4, 9, 2]]
print(sorted(x, key=list))

Question 02

from math import sqrt
p=[(2, 1), (3, 1), (1, 5), (1, 0), (4, 2)]
print(sorted(p, key=lambda p: sqrt((5-p[0])**2 + (5-p[1])**2) ))


Question 03
import csv

courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
with open('studentsData.csv', 'r') as f:
r=csv.DictReader(f)
data=[]
d={}
for rec in r:
d={}
for k, v in rec.items():
if(k in ['Reg', 'Name', 'Sec']):
d[k]=v
if (k in courses and v!='NR'):
if ('Courses' not in d):
d['Courses']={k:v}
else:
d['Courses'][k]=v
data.append(d)

courses=['DLD', 'CP', 'ED', 'VCA', 'EM']
CH=[3, 2, 3, 3, 2]
subjects=dict(zip(courses, CH))

gLetter=['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'F']
gPoints=[4.0, 4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.7, 1.3, 1.0, 0]
gDict=dict(zip(gLetter, gPoints))

def calculateGPA(a):
l=[subjects[i]*gDict[j] for i, j in a['Courses'].items()]
return sum(l)/sum(CH)

a=sorted(data, key=calculateGPA)
print([i['Reg']for i in a])


Question 04
d=[2, 5, -1, 4, -2, 1, 0]
print(sorted(d, key=abs))
print(sorted(d, key=lambda d:(abs(d), d>0), reverse=True))

farazshah