您的位置:首页 > 其它

simple cases about functional programming

2015-10-30 15:57 453 查看
import functools

def fac(n):
return functools.reduce(lambda a,b:a*b, range(1,n+1))

fac(5)

120


another solution is using operator module

import operator
def fact(n):
return functools.reduce(operator.mul, range(1,n+1))

fact(5)

120

metro_data = [
('Tokyo', 'JP', 36.933, (35.689722, 139.691667)),
('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889)),
('Mexico City', 'MX', 20.142, (19.433333, -99.133333)),
('New York-Newark', 'US', 20.104, (40.808611, -74.020386)),
('Sao Paulo', 'BR', 19.649, (-23.547778, -46.635833))
]

for city in sorted(metro_data, key=operator.itemgetter(1)):   # 根据国籍排序 使用itemgetter函数
print(city)

('Sao Paulo', 'BR', 19.649, (-23.547778, -46.635833))
('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889))
('Tokyo', 'JP', 36.933, (35.689722, 139.691667))
('Mexico City', 'MX', 20.142, (19.433333, -99.133333))
('New York-Newark', 'US', 20.104, (40.808611, -74.020386))

country_city_name = operator.itemgetter(1,0)  # an function get values from index 1 and 0 , then collect them in tuple
for ele in map(country_city_name, metro_data):
print(ele)

('JP', 'Tokyo')
('IN', 'Delhi NCR')
('MX', 'Mexico City')
('US', 'New York-Newark')
('BR', 'Sao Paulo')


由于itemgetter利用了[]操作符,所以它支持任何实现了__getitem__方法的类型,不局限于序列还包括字典。

类似的还有attrgetter,它也是个函数,以属性名称(字符串类型)作为参数,创建函数,最终作用于实例对象,返回tuple类型。

任何能被.(dot)访问的属性都能被attrgetter访问。

import collections
LatLong = collections.namedtuple('LatLong', 'lat long')
Metropolis = collections.namedtuple('Metropolis', 'name cc pop coord')

metro_areas = [Metropolis(name, cc, pop, LatLong(lat, long)) for name, cc, pop, (lat, long) in metro_data]

metro_areas[0]

Metropolis(name='Tokyo', cc='JP', pop=36.933, coord=LatLong(lat=35.689722, long=139.691667))

metro_areas[0].coord.lat

35.689722

name_lat = operator.attrgetter('name','coord.lat')   # a function get attribute named 'name', 'coord.lat'
for city in sorted(metro_areas, key=operator.attrgetter('coord.lat')): # sort use the attribute 'coord.lat'
print(name_lat(city))

('Sao Paulo', -23.547778)
('Mexico City', 19.433333)
('Delhi NCR', 28.613889)
('Tokyo', 35.689722)
('New York-Newark', 40.808611)

[name for name in dir(operator) if not name.startswith('_')]

['abs',
'add',
'and_',
'attrgetter',
'concat',
'contains',
'countOf',
'delitem',
'eq',
'floordiv',
'ge',
'getitem',
'gt',
'iadd',
'iand',
'iconcat',
'ifloordiv',
'ilshift',
'imod',
'imul',
'index',
'indexOf',
'inv',
'invert',
'ior',
'ipow',
'irshift',
'is_',
'is_not',
'isub',
'itemgetter',
'itruediv',
'ixor',
'le',
'length_hint',
'lshift',
'lt',
'methodcaller',
'mod',
'mul',
'ne',
'neg',
'not_',
'or_',
'pos',
'pow',
'rshift',
'setitem',
'sub',
'truediv',
'truth',
'xor']

s = 'The time has come'

upcase = operator.methodcaller('upper') # a function
print(upcase(s))

THE TIME HAS COME

replce_maker = operator.methodcaller('replace',' ','-')
print(replce_maker(s))

The-time-has-come

triple = functools.partial(operator.mul, 3)

triple(7)

21

list(map(triple,range(1,11)))

[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]

import unicodedata

nfc = functools.partial(unicodedata.normalize,'NFC')

s1 = 'café'

s2 = 'cafe\u0301'

s1

'café'

s2

'café'

s2 == s1

False

nfc(s1)

'café'

nfc(s2)

'café'

nfc(s1) == nfc(s2)

True
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: