利用Python进行数据分析

函数是对象

2018-12-30  本文已影响0人  庵下桃花仙

Python 的函数是对象,假设对数据进行清洗。

In [1]: states = ['  Alabama', 'Georgia!', 'Georgia', 'georgia', 'F10rIda',
   ...: 'south  carolina##', 'West virginia?']
In [9]: def clean_strings(strings):
   ...:     result = []
   ...:     for value in strings:
   ...:         value = value.strip()
   ...:         value = re.sub('[!#?]', '', value)
   ...:         value = value.title()
   ...:         result.append(value)
   ...:     return result
   ...:

In [10]: clean_strings(states)
Out[10]:
['Alabama',
 'Georgia',
 'Georgia',
 'Georgia',
 'F10Rida',
 'South  Carolina',
 'West Virginia']

另一个有用的实现,是将特定的列表操作应用到字符串集合上。

In [11]: def remove_punctuation(value):
    ...:     return re.sub('[!#?]', '', value)
    ...:

In [12]: clean_ops = [str.strip, remove_punctuation, str.title]

In [13]: def clean_strings(strings, ops):
    ...:     result = []
    ...:     for value in strings:
    ...:         for function in ops:
    ...:             value = function(value)
    ...:         result.append(value)
    ...:     return result
    ...:

In [14]: clean_strings(states, clean_ops)
Out[14]:
['Alabama',
 'Georgia',
 'Georgia',
 'Georgia',
 'F10Rida',
 'South  Carolina',
 'West Virginia']

这种更为函数化的模式,便于更高层次上修改字符串变换方法。

可以将函数作为一个参数传递给其他函数,如 map 函数。

In [15]: for x in map(remove_punctuation, states):
    ...:     print(x)
    ...:
  Alabama
Georgia
Georgia
georgia
F10rIda
south  carolina
West virginia

map 函数

map(function, iterable, ...)
function -- 函数
iterable -- 一个或多个序列
Python 3.x 返回迭代器。

In [16]: def square(x):
    ...:     return x ** 2
    ...:

In [17]: map(square, [1, 2, 3, 4, 5])
Out[17]: <map at 0x5108128>

In [18]: map(lambda x: x ** 2, [1, 2, 3, 4, 5])
Out[18]: <map at 0x53800f0>

In [19]: map(lambda x, y: x + y, [1, 2, 3], [4, 5, 6])
Out[19]: <map at 0x5387fd0>
上一篇下一篇

猜你喜欢

热点阅读