(python study) for a loop (example source)


print('-- for example')

l = ['Apple', 100, 15.23]
for i in l:
 print(i, type(i))


print('-- dictionary')

d = {"Apple":100, "Orange":200, "Banana":300}
for k, v in d.items(): #print dictionary
 print(k, v)


print('-- iterator')

l = [10, 20, 30]
iterator = iter(l)
for i in iterator:
 print(i)


print('-- 9*9 ')

for n in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
 print("-- {0} step --".format(n))
 for i in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
  print("{0} * {1} = {2}".format(n, i, n*i))


print('-- break, continue and else example')

l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in l:
 if i > 5:
  break
 print("Item:{0}".format(i))


for i in l:
 if i % 2 == 0:
  continue
 print("Item: {0}".format(i))


for i in l:
 if i > 5:
  break
 print("Item:{0}".format(i))
else:
 print("Exit without break")
print("Always this is printed")


print("-- useful function")

list( range(10) )
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list( range(5,10) )
#[5, 6, 7, 8, 9]
list( range(10, 0, -1))
#[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
list( range(10, 20 , 2))
#[10, 12, 14, 16, 18]

for i in range(10, 20, 2):
 print(i)



print("-- to get index and value concurrently")

l = ['Apple', 'orange', 'Banana']
for i in range(len(l)):
 print("index:{0}, value:{1}".format(i, l[i]))


print("-- enumerate example")
l = ["Apple", 100, 15.5]
for i in enumerate(l):
 print(i)

for i in enumerate(l, 300):
 print(i)


#list comprehensions
l = [1, 2, 3, 4, 5]
print( [i ** 2 for i in l] )
#[1, 4, 9, 16, 25]

t = ("apple", "banana", "orange")
print( [len(i) for i in t] )
#[5, 6, 6]

d = {100:"apple", 200:"banana", 300:"orange"}
print( [v.upper() for v in d.values() ] )

print( [i ** 3 for i in range(5)] )
#[0, 1, 8, 27, 64]

print( [i for i in t if len(i) > 5 ] )
#['banana', 'orange']

l1 = [3, 4, 5]
l2 = [1.5, -0.5, 4]
print( [x*y for x in l1 for y in l2 ] )
#[4.5, -1.5, 12, 6.0, -2.0, 16, 7.5, -2.5, 20]


print("-- filter example")
l=[10, 25, 30]
IterL = filter(None, l)
for i in IterL:
 print("Item: {0}".format(i))

def GetBiggerThan20(i):
 return i > 20

print("---")

IterL = filter(GetBiggerThan20, l)
for i in IterL:
 print("Item: {0}".format(i))

print("using lambda")
IterL = filter(lambda i: i>20, l)
for i in IterL:
 print("Item: {0}".format(i) )

print("-- zip")

X = [10, 20, 30]
Y = ['A', 'B', 'C']
for i in zip(X, Y):
 print("Item: {0}".format(i))

RetList = list(zip(X,Y))
[(10, 'A'), (20, 'B'), (30, 'C')]
#unzip
X2, Y2 = zip(*RetList)

X = [10, 20, 30]
Y = "ABC"
Z = (1.5, 2.5, 3.5)
RetList = list(zip(X, Y, Z))

print("another example for zip")

X = [10, 20, 30]
Y = "ABCD"
RetList = list(zip(X, Y))
#[(10, 'A'), (20, 'B'), (30, 'C')]

print("-- map function example")

L = [1, 2, 3]

def Add10(i):
 return i+10

for i in map(Add10, L):
 print("Item: {0}".format(i) )

RetList = list( map((lambda i: i+10), L))
print(RetList)

X = [1, 2, 3]
Y = [2, 3, 4]
RetList = list(map(pow, X, Y))
print(RetList)

print("--join function example")

l = ['Apple', 'Orange', 'Banana']
for i in l:
 print(i)

#--> join form
print("\n".join(l))


print("-- processing time compare with noraml print and joint")

import time

l = range(1000)

t = time.mktime(time.localtime())
for i in l:
 print(i, )
t1 = time.mktime(time.localtime()) - t

t = time.mktime(time.localtime())
print(",".join(str(i) for i in l))
t2 = time.mktime(time.localtime()) - t

print("for -> Take {0} seconds".format(t1))
print("join -> Take {0} seconds".format(t2))

#result is 0.0 second both.. -.-














Comments

Popular posts from this blog

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

OpenCV Stitching example (Stitcher class, Panorama)

AMP dose run on my pc?, AMP(Accelerated Massive Parallelism)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Optical Flow sample source code using OpenCV

Real-time N camera stitching Class.

OpenCV meanShiftFiltering example source code ( cpu: pyrMeanShiftFiltering, gpu:meanShiftFiltering, gpu:meanShiftSegmentation )

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Image warping (using opencv findHomography, warpPerspective)

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)