Tuesday, July 20, 2021

lambda функцийн жишээнүүд

 


# 10 дээр а аргумент нэмэх ламбда функц
x = lambda a : a +10
print(x(5))

x = lambda a, b : a * b
print(x(5, 6))

def myfunc(n):
    return lambda a : a * n

mydoubler = myfunc(2)
mythipler = myfunc(3)

print(mydoubler(11))
print(mythipler(11))

def newfunc(k):
    return lambda a : a - k

new1 = newfunc(7)
new2 = newfunc(3)

print(new1(11))
print(new2(11))

list_01 = list(map(lambda x : x.capitalize(), ['cat', 'dog', 'cow', 'sheep', 'goat', 'horse', 'camel']))
print(list_01)

even = lambda x : x % 2 == 0
print(list(filter(even, range(11))))

full_name = lambda first, last : f'Бүтэн нэр : {first.title()} {last.title()}'
print(full_name('банзрагч', 'цогбадрах'))

ids = ['id1', 'id2', 'id30', 'id3', 'id22', 'id100']
print(sorted(ids)) # Lexicographic sort
sorted_ids = sorted(ids, key=lambda x: int(x[2:])) # Integer sort
print(sorted_ids)

No comments:

Post a Comment