pythonmapif
Ⅰ python map和rece的用法
map(function, sequence[, sequence, ...]) -> list
Return a list of the results of applying the function to the items of
the argument sequence(s). If more than one sequence is given, the
function is called with an argument list consisting of the corresponding
item of each sequence, substituting None for missing values when not all
sequences have the same length. If the function is None, return a list of
the items of the sequence (or a list of tuples if more than one sequence).
rece(...)
rece(function, sequence[, initial]) -> value
Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to rece the sequence to a single value.
For example, rece(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.
Ⅱ 假設python沒有提供map()函數,請自行編寫一個my_map()函數實現與map()相同的
#python的map,filter,rece等函數復都是為制了簡化,方便循環list的函數。
#所以如果不用的話,就相當於把for循環展開
L=[1,2,3,4,5]
defmy_map(L):
result=[]
foreinL:
result.append(e*2+1)
returnresult
printmap(lambdax:x*2+1,L)#輸出[3,5,7,9,11]
printmy_map(L)#輸出[3,5,7,9,11]
#不用函數
print[x*2+1forxinL]#輸出[3,5,7,9,11]
#不用函數計算大於等於3的
print[x*2+1forxinLifx>=3]#輸出[7,9,11]
#使用mapfilter計算大於等於3的,
printmap(lambdax:x*2+1,filter(lambdax:x>=3,L))#輸出[7,9,11]
Ⅲ python maprece 怎麼統計相同字元串出現的次數
# -*- coding: utf-8 -*-str_='ssdasdasefadd'dict_char_tmp = {i:str_.count(i) for i in str_} #得到所有單詞的個數print '得到所有單詞的個數:', dict_char_tmpdict_char={}for k,v in dict_char_tmp.items(): if dict_char.get(v): dict_cha...
Ⅳ 如何在python3.3用 map filter rece
#!/usr/bin/envpython
#coding=utf-8
"""
map,filter,receinpython3
"""
fromfunctoolsimportrece
deftest_main():
assertlist(range(10))==[0,1,2,3,4,5,6,7,8,9]
assertlist(map(lambdax:x+1,range(10)))==[1,2,3,4,5,6,7,8,9,10]
assertrece(lambdax,y:x+y,range(10))==45
assertlist(filter(lambdax:x>5,range(10)))==[6,7,8,9]
defmain():
test_main()
if__name__=='__main__':
main()
只有rece被移到functools模塊裡面了。其它沒什麼大變。
Ⅳ python運行時出現「TypeError: 'map' object is not subscriptable」錯誤
有問題就google,一般能找到解決的辦法內。
map() doesn't return a list, it returns a map object.
You need to call list(map) if you want it to be a list again.
原文容:http://stackoverflow.com/questions/6800481/python-map-object-is-not-subscriptable
Ⅵ 怎麼用Python寫maprece,請舉例說明,初學者,請賜教,不勝感激
1.lambda
#匿名函數
#基本用法lambdax:x**2
#第一個參數,然後是表達式
#也可以使用如下
(lambdax:x**2)(5)
2.map()
defmap(function,sequence,*sequence_1):#realsignatureunknown;restoredfrom__doc__
"""
map(function,sequence[,sequence,...])->list
theargumentsequence(s).Ifmorethanonesequenceisgiven,the
itemofeachsequence,
sequenceshavethesamelength.IfthefunctionisNone,returnalistof
theitemsofthesequence().
"""
return[]
#兩個參數,一個處理函數,一個可迭代的序列
#返回一個列表
#例如計算1到10的平方,並以列表的形式返回
map(lambdax:x**2,range(1,11))
#結果如下
[1,4,9,16,25,36,49,64,81,100]
#當然也可以如下這樣使用
defsquare(x):
returnx**2
map(square,range(1,11))
3.rece()
defrece(function,sequence,initial=None):#realsignatureunknown;restoredfrom__doc__
"""
rece(function,sequence[,initial])->value
,
fromlefttoright,.
Forexample,rece(lambdax,y:x+y,[1,2,3,4,5])calculates
((((1+2)+3)+4)+5).Ifinitialispresent,itisplacedbeforetheitems
ofthesequenceinthecalculation,andservesasadefaultwhenthe
sequenceisempty.
"""
pass
#兩個參數,一個接受兩個參數的函數,一個序列參數
#例如計算1到10的和
rece(lambdax,y:x+y,range(1,11))
#當然,不適用lambda匿名函數也可以
defadd(x,y):
returnx+y
rece(add,range(1,11))
#結果如下
45
4.filter()
deffilter(function_or_none,sequence):#knownspecialcaseoffilter
"""
filter(functionorNone,sequence)->list,tuple,orstring
(item)istrue.If
functionisNone,returntheitemsthataretrue.Ifsequenceisatuple
orstring,returnthesametype,elsereturnalist.
"""
pass
#接受兩個參數,一個過濾函數,返回True或者False,以及一個序列
#例如,計算100以內的偶數
filter(lambdax:x%2==0,range(100))
#如上
defdiv2(x):
ifx%2==0:
returnTrue
else:
returnFalse
filter(div2,range(100))
#結果如下
[0,2,4,6,8,10,12,14,16,...]
Ⅶ Python3.5.2中一個函數a().有時候不帶括弧直接用a.如map(a,「」)這里a什麼意思
我有一個初步理解,但不知道底層細節,python中函數名也是個變數,可以說你定義的所有名稱都是變數,所有的變數你理解成一個指針,指向一個空間,傳遞名稱則傳遞指針,給其他函數這個空間的開始。那麼這個()呢,在py中有個call方法,你看不到,這個括弧會執行call,call會運行解釋你的代碼。這樣,一個函數才算是真正執行了。
Ⅷ python怎麼構建hash map
字典
python里的字典就像java里的HashMap,以鍵值對的方式存在並操作,其特點如下
通過鍵來存取,而非偏移量;
鍵值對是無序的;
鍵和值可以是任意對象;
長度可變,任意嵌套;
在字典里,不能再有序列操作,雖然字典在某些方面與列表類似,但不要把列表套在字典上
點擊(此處)折疊或打開
#coding:utf-8
#!/usr/bin/python
# Filename: map.py
table = {'abc':1, 'def':2, 'ghi':3}
print table
#字典反轉
map=dict([(v,k) for k, v in table.iteritems()])
#字典遍歷
for key in map.keys():
print key,":",map[key]
print len(map)
print map.keys()
print map.values()
#字典的增,刪,改,查
#在這里需要來一句,對於字典的擴充,只需定義一個新的鍵值對即可,
#而對於列表,就只能用append方法或分片賦值。
map[4]="xyz"
print map
del map[4]
print map
map[3]="update"
print map
if map.has_key(1):
print "1 key in"
{'abc': 1, 'ghi': 3, 'def': 2}
1 : abc
2 : def
3 : ghi
3
[1, 2, 3]
['abc', 'def', 'ghi']
{1: 'abc', 2: 'def', 3: 'ghi', 4: 'xyz'}
{1: 'abc', 2: 'def', 3: 'ghi'}
{1: 'abc', 2: 'def', 3: 'update'}
1 key in
Ⅸ python中map(lambda x: x % mydict, mylist)是什麼意思
其實這句話,你應該理解map和lambda是什麼意思。
1. lambda生成匿名函數
2. map(func,seq) 就是將函數作用在序列的每個元素上,然後創建由函數返回值組成的列表。
3. map(lambda x: x % mydict, mylist),遍歷mylist每個元素,執行lambda函數,並返回一個列表
Ⅹ python使用了for,if,elif,else語句,定義一個比較大小的函數
方法用錯了, 首先不能是DataFrame的applymap方法 這個方法是對每一個元素進行處理的
其次, 處理函數(你的func_wd)是接受一個值, 返回一個值, 不是接受一堆值然後循環
def func_wd(x) 這個x就是wd那一列中的某一個值, 裡面直接分支返回就行了:
在map這個方法的時候, 用Series的apply:
data['wd'] = data['wd'].apply(fuc_wd)
這樣就行了