• 函数小练习


    # (1)
    name = ['oldboy', 'alex', 'wusir']
    def func(n):	
    	return n+"_leader"
    	
    # map 返回迭代器
    # None 地址 容器 迭代器 map(func,name)
    it = map(func,name)
    print(list(it))
    
    # 匿名函数
    it = map(lambda n : n+'_leader',name)
    print(list(it))
    
    
    # 2.用map来处理下述 listvar ,要求得到新列表,每个元素名字加后面加_leader
    # listvar = [{'name':'alex'},{'name':'wusir'}]
    listvar = [{'name':'alex'},{'name':'wusir'}]
    def func(n):
    	# n = {'name':'alex'}
    	return n['name'] + "_leader"
    	
    it = map(func,listvar)
    print(list(it))
    
    it = map(lambda n : n['name'] + "_leader",listvar)
    print(list(it))
    
    # 3.用filter来处理,得到股票价格大于20的股票名字
    shares={
       	'IBM':36.6,
       	'Lenovo':23.2,
      	'oldboy':21.2,
        'ocean':10.2,
    	}
    
    def func(n):
    	# n => IBM Lenovo oldboy ocean
    	# return shares[n] 
    	if shares[n] > 20:
    		return True	
    it = filter(func,shares)
    print(list(it))
    
    # 匿名函数
    it = filter(lambda n : True if shares[n] > 20 else False , shares)
    print(list(it))
    """
    4.有下面字典:
    """
    portfolio=[
    	{'name':'IBM','shares':100,'price':91.1},
    	{'name':'AAPL','shares':20,'price':54.0},
    	{'name':'FB','shares':200,'price':21.09},
    	{'name':'HPQ','shares':35,'price':31.75},
    	{'name':'YHOO','shares':45,'price':16.35},
    	{'name':'ACME','shares':75,'price':115.65}
    ]
    # a.获取购买每只股票的总价格(乘积),迭代器中[9110.0, 1080.0 ,......]
    # b.用filter过滤出price大于100的股票。1
    # (a)
    def func(n):
    	# n =>{'name':'IBM','shares':100,'price':91.1}
    	return n['shares'] * n['price']
    
    it = map(func,portfolio)
    print(list(it))
    
    # 匿名函数
    it = map(lambda n : n['shares'] * n['price'],portfolio)
    print(list(it))
    
    #(b)
    def func(n):
    	if n['price'] > 100:
    		return True
    
    it = filter(func,portfolio)
    print(list(it))
    
    # 匿名函数
    it = filter(lambda n : True if n['price'] > 100 else False,portfolio)
    print(list(it))
    
    # 5. 
    listvar = [
    	{'sales_volumn': 0},
    	{'sales_volumn': 108},
    	{'sales_volumn': 337},
    	{'sales_volumn': 475},
    	{'sales_volumn': 396},
    	{'sales_volumn': 172},
    	{'sales_volumn': 9},
    	{'sales_volumn': 58},
    	{'sales_volumn': 272},
    	{'sales_volumn': 456},
    	{'sales_volumn': 440},
    	{'sales_volumn': 239}
    ]
    
    # 将 listvar 按照列表中的每个字典的values大小进行排序,形成一个新的列表。
    def func(n):
    	# n {'sales_volumn': 0}
    	return n['sales_volumn']
    lst = sorted(listvar,key=func)	
    print(lst)
    '''
    {'sales_volumn': 0} => 0
    {'sales_volumn': 108} => 108
    {'sales_volumn': 337} => 337
    ...
    ...
    依次类推:按照values排序,之后对列表排序
    '''
    
    # 匿名函数
    lst= sorted(listvar,key=lambda n : n['sales_volumn'] )
    print(lst)
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    

      

  • 相关阅读:
    php 数据库练习之租房子
    php数据访问之查询关键字
    Objective-C代码学习大纲(3)
    Objective-C代码学习大纲(2)
    Objective-C代码学习大纲(1)
    简介Objective-C语言
    为什么Objective-C很难
    Swift之 ? 和 !
    使用Mac App Store更新、下载软件时出现未知错误的解决方法
    如何激励用户为你的app评分?
  • 原文地址:https://www.cnblogs.com/huangjiangyong/p/10906566.html
Copyright © 2020-2023  润新知