Mighty-Taungoo 5.1.4, 5.2 ,5.6

5.1.4. Nested List Comprehensions
matrix = [
[1, 2, 3, 4] ,
[5, 6, 7, 8] ,
[9, 10, 11, 12] ,
]
For eg. [ [A[i] for A in matrix] for i in range(4) ] #range(4) print from 0 to 3
#[ [1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12] ]

For eg. T = [ ] #struct empty array
for i in range(4): # range(4) print 0 to 3.
T.append( [ A[i] for A in matrix] ) #added each column into T matrix.
#[ [1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12] ]

list( zip (*matrix) ) # if `*` contain and print the column in matrix
#[ (1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12) ]
#if does not `*` contain and print the row in matrix
#[ (1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12) ]

‌5.2del statement
‌ del statementဆိုတာlistထဲမှာရိှတဲ့item​တွေကိုremoveလုပ်​တဲ့methodတစ်​ခုပါ.del method ဟာ array ထဲက value ​တွေကိုreturn ပြန်​​ပေးပါတယ်​.e.g
A = [-1, 1, 10, 33, 12, 25.6] #assign into A(name)array/ array index0 to 5
del A[0]
A#call array
[1, 10, 33, 12, 25.6]#output
A[1:3]#index 1 to 2 removed
A#call array
[1, 12, 25.6]#output
del A[:]#all remove item in array A
A#call array
[]#output
‌5.6Looping Techniques
‌ items()method ဆိုတာinputထည့်ထားတဲ့​value​တွေကို loopingသုံးပြီ​တစ်ချိ​န်​ထဲမှာအစီစဉ်​တကျထုတ်​​ပေးနိုင်​တဲ့methodတစ်​ခုဖြစ်​ပါတယ်​.e.g
‌king = {`gallahad´ : `the pure´, `robin´: `the brave´, `the´: `job´}#assign into king(variable)
for k, v, c in king.item():#using "for" looping and item method
‌ print(k, v, c)#output with print method
gallahad the pure
robin the břave
the job
‌enumerate() function
‌ enumerate() functionကိုသုံးပြီးinputထည်​့ခဲ့မယ်​ဆိုရင်​output ထုတ်​တဲ့ခါposition ​ရော value​ရောတစ်​ပြိုင်​ထဲထုတ်​​ပေးပါတယ်​.e.g
for i, v in enumerate([`tic´, `pic´,`pot´]):#using "for"loop and enumerate() function assign into array# "i"is index number and "v" is value
print(i, v)#output with print method
0 tic
1 pic
2 pot
‌zip() function
‌ ‌ zip() functionဆိုတာ array ​တွေကိုassignလုပ်​ပြီးarray index ​တွေကိုတွဲ​ပေးနိုင်​တဲ့functionတစ်​ခုဖြစ်​ပါတယ်​.e.g
questions = [`name´, ` quest´, `favorite color´]#assign into questions array
answers = [` lancelot´ ,`the holy grail´, `blue´]#assign into answers array
for q, a in zip(questions, answers):#using "for" loop and "questions" define "q" and "answer" define "a" and using zip()function
print(` What is your { 0}? It is {1}.´.format(q, a))#using print method
#output
What is your name? It is lancelot.
What is your quest? It is the holy grail.
What is your favorite color? It is blue.
‌ zip() in conjunction with the * operator can be used to unzip a list.e.g
x = [1, 2, 3] #assign x array
y = [4, 5, 6] #assign y array
zipped = zip(x, y)#zip() function assign into zipped
list (zipped) #call zipped in list
[(1, 4) , (2, 5), (3, 6)]#output
x2, y2 = zip(*zip(x, y)) #using *operator unzip into zip(x, y) assign x2, y2
x == list(x2) and y == list(y2) #using list()method x equal x2 and y equal y2
True #output
‌ reversed () function
reversed() function ဆိုတာarray ရဲ့​နောက်​ဆုံးအခန်​းက​နေစပြီး​တော့ထုတ်​​ပေးတဲ့ functionတစ်​ခုဖြစ်​တယ်​.e.g
for i in reversed(range(1, 10, 2)):#using for loop and reversed() function in range(start point, before end point, differ between point)
print(i)
#output
9
7
5
3
1
‌sorted() function
basket = [` apple´, `orange´, `apple´, `pear´, `orange´, `banana´]#assign into basket array
for f in sorted(set(basket)): #using for looping and basket array into "set"(setဆိုတာarray ထဲကိုassignလုပ်​ထားတဲ့value ​တွေထဲကတူတာ​တွေဆိုတစ်​ခါဘဲထုတ်​​ပေးတယ်​)sorted into set(basket)
print(f)
#output
apple
banana
orange
pear
e.g
import math #implement math
d = [56.2 , float(`NaN´), 51.7, 55.3 , 52.5 , float(`NaN´) , 47.8]# assign into d array #float is decimal point #NaN is not a number
f = [ ] #assign into f blank array
for value in d: # using for looping with d
if not math.isnan(value):#using if looping and isnan() function
f.append(value)#using append() method and value added into f
f
#output
‌[56.2, 51.7, 55.3, 52.5, 47.8]
Sign In or Register to comment.