日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python数据分析函数大全_Python常用数据分析函数集合

發布時間:2024/7/23 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python数据分析函数大全_Python常用数据分析函数集合 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.Map函數 - 列表解析

①.map()函數解析

(1).python源碼信息

C:\Users\ArSang>python

Python3.6.3rc1 (v3.6.3rc1:d8c174a, Sep 19 2017, 16:39:51) [MSC v.1900 64bit (AMD64)] on win32

Type"help", "copyright", "credits" or "license" formore information.>>>help(map)

Help on class mapinmodule builtins:

class map(object)| map(func, *iterables) -->map object|

| Make an iterator that computes the functionusing arguments from|each of the iterables. Stops when the shortest iterable is exhausted.|

|Methods defined here:|

| __getattribute__(self, name, /)| Returngetattr(self, name).|

| __iter__(self, /)|Implement iter(self).|

| __new__(*args, **kwargs) from builtins.type| Create and return a new object. See help(type) foraccurate signature.|

| __next__(self, /)|Implement next(self).|

|__reduce__(...)| Return state information for pickling.

View Code

(2).map()接收一個函數和一個可迭代對象list數組,并通過函數方法去迭代list中的每個元素從而得到并返回一個新的list

p_list = [1, 2, 3, 4]#構建函數

deff(x):"""實現給list中的每個元素+2

:return:"""

return x + 2

'''3.x中返回需要添加返回類型,spe=list(),2.x中不需要,在3.0中map函數僅僅是創建一個待運行的命令容器,只有其他函數調用他的時候才會返回結果

Python 2.x 返回列表 Python 3.x 返回迭代器'''spe=list(map(f, p_list))print(spe) #返回新的list

print(id(p_list)) #查詢原數組內存地址

print(id(spe)) #查詢新數組內存地址

"""spe≠p_list"""

View Code

(3).使用for循環迭代實現

l = [1, 2, 3, 4]defadd_list(x):return x + 2

defmap_list(f, arr_list):

temp=[]for i inarr_list:

rep=f(i)

temp.append(rep)returntemp

res=map_list(add_list, l)print(res) #返回新的數組

print(id(l)) #查詢原數組內存地址

print(id(res)) #查詢新數組非常地址

View Code

(4).列表解析for與map

'''Python下:for效率 < map()效率'''

②.map()函數也可接受多參數的函數

l = [1, 2, 3, 4]#將l列表加2后返回一個新列表

rep = map(lambda x: x + 2, l)print(list(rep))

a= [1, 2, 3, 4]

b= [5, 6, 7, 8]#將a,b兩個數組中的元素相乘,返回一個新的列表元素

ret_list = map(lambda x, y: x *y, a, b)print(list(ret_list))

③普遍函數與匿名方法:

a = [1, 2, 3, 4]

b= [5, 6, 7, 8]defadd_fun(number1, number2):

number1+=number2returnnumber1defsum_list():#普遍函數

res1 =map(add_fun, a, b)#匿名函數

res2 = map(lambda x: x ** 2, [x for x in range(10)])print(list(res1), list(res2))if __name__ == '__main__':

sum_list()

View Code

④其他應用

a = ['YSDSASD', 'lsfrr', 'tGdDSd', 'Sdddd']deffun(f):"""將列表的第一個字母偶同意大寫,后面的字母統一小寫

:return:"""

return f[0:1].upper() + f[1:].lower()if __name__ == '__main__':

lit=map(fun, a)print(list(lit))

View Code

2.reduce()函數 - 遞歸計算

①reduce()函數解析

(1).python3中源碼解析

from functools importreduceprint(help(reduce))

reduce(...)

reduce(function, sequence[, initial])->value

Apply a function of two arguments cumulatively to the items of a sequence,fromleft to right, so as to reduce the sequence to a single value.

For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates

((((1+2)+3)+4)+5). If initial is present, it isplaced before the items

of the sequencein the calculation, andserves as a default when the

sequenceis empty.

View Code

(2).reduce()函數取值規則:

#python3中使用reduce需要導入

from functools importreduce

a= [1, 2, 3, 4]

app_list= reduce(lambda x, y: x *y, a)

num_list= reduce(lambda x, y: x * y, range(1, 5))print(app_list)print(num_list)'''a = [1, 2, 3, 4]

第一次相乘:x([0])*y([1])

第二次相乘:x(([0]*[1])result)*y([2])

第三次相乘:x(([0]*[1]*[2])result)*y([3])

.....'''

View Code

②其他應用

#python3中使用reduce需要導入

from functools importreduce

a= [1, 2, 3]'''x * y的單結果+1

第一次計算:1 *2 + 1 = result

第二次計算:(result*3)+1 = resulttwo

第三次計算:(resulttwo*4)+1

....'''app_list= reduce(lambda x, y: x * y+1, a)#第三參數:5 x*y的總結果*5

num_list = reduce(lambda x, y: x * y, range(1, 5), 5)print(app_list)print(num_list)

View Code

3.filter()函數 - 過濾器

①filter()函數解析

(1).python源碼解析

C:\Users\ArSang>python

Python3.6.3rc1 (v3.6.3rc1:d8c174a, Sep 19 2017, 16:39:51) [MSC v.1900 64bit (AMD64)] on win32

Type"help", "copyright", "credits" or "license" formore information.>>>help(filter)

Help onclass filter inmodule builtins:classfilter(object)| filter(function or None, iterable) -->filter object|

| Return an iterator yielding those items of iterable forwhich function(item)| is true. If function is None, returnthe items that are true.|

|Methods defined here:|

| __getattribute__(self, name, /)|Return getattr(self, name).|

| __iter__(self, /)|Implement iter(self).|

| __new__(*args, **kwargs) frombuiltins.type| Create and return a new object. See help(type) foraccurate signature.|

| __next__(self, /)|Implement next(self).|

| __reduce__(...)| Return state information for pickling.

View Code

②filter()返回值

#列表解析的方式

b = [i for i in range(10) if i > 5 and i < 8]print(list(b))#filter()函數方式

b = filter(lambda x: x > 5 and x < 8, range(10))print(list(b))'''1.filter()函數首先需要返回一個bool類型的函數

2.如上示例,判斷x是否大于5且小于8,最后將這個函數作用到range(10)的每個函數中,如果為True,則將滿足條件的元素組成一個列表返回'''

View Code

③其他應用

(1).刪除None列表元素

defis_empty(s):"""刪除None元素字符

:return:"""

return s and len(s.strip()) >0if __name__ == '__main__':

b= ['', 'str', ' ', 'end', '', '']print(list(filter(is_empty, b)))

View Code

(2).匿名函數和自定義函數

importrandom#自定義函數

'''def fun(x):

return x * 2

arr_list = []

for i in range(10):

arr_list.append(random.randint(1, 20))

print(arr_list)

if __name__ == '__main__':

print(list(filter(fun, arr_list)))'''

#匿名函數

arr_lst =[]for i in range(10):

arr_lst.append(random.randint(1, 20))print(arr_lst)if __name__ == '__main__':print(list(filter(lambda x: x * 2, arr_lst)))

View Code

總結

以上是生活随笔為你收集整理的python数据分析函数大全_Python常用数据分析函数集合的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。