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)
这样就行了