码迷,mamicode.com
首页 > 其他好文 > 详细

lambda map() filter() zip()练习

时间:2019-07-22 19:52:49      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:tool   res   lenovo   列表   lam   字符串   cti   count   过滤   

练习:

  1. 用map来处理字符串列表,把列表中所有人都变成sb,比方alex_sb

    l=[{'name':'alex'},{'name':'y'}]
    l=[{'name':'alex'},{'name':'y'}]
    l = list(map(lambda x:{"name":x["name"]+"_sb"},l))
    print(l)
  2. 用map来处理下述l,然后用list得到一个新的列表,列表中每个人的名字都是sb结尾

l=[{'name':'alex'},{'name':'y'}]
l=[{'name':'alex'},{'name':'y'}]
l1 = list(map(lambda x:{"name":x["name"]+"sb"},l))
print(l1)
  1. 用filter来处理,得到股票价格大于20的股票名字
shares={
 'IBM':36.6,
 'Lenovo':23.2,
 'oldboy':21.2,
 'ocean':10.2,
         }
shares={
 'IBM':36.6,
 'Lenovo':23.2,
 'oldboy':21.2,
 'ocean':10.2,
         }
lst = list(filter(lambda x:shares[x]>20,shares))
print(lst)
  1. 有下面字典,得到购买每只股票的总价格,并放在一个迭代器中。
    结果:list一下[9110.0, 27161.0,......]
portfolio = [
  {'name': 'IBM', 'shares': 100, 'price': 91.1},
    {'name': 'AAPL', 'shares': 50, 'price': 543.22},
    {'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}]
portfolio = [
  {'name': 'IBM', 'shares': 100, 'price': 91.1},
    {'name': 'AAPL', 'shares': 50, 'price': 543.22},
    {'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}]
lst = map(lambda x: x["shares"]*x["price"],portfolio)
print(list(lst))
  1. 还是上面的字典,用filter过滤出单价大于100的股票。

    portfolio = [
      {'name': 'IBM', 'shares': 100, 'price': 91.1},
        {'name': 'AAPL', 'shares': 50, 'price': 543.22},
        {'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}]
    lst = filter(lambda x:x["shares"]>100,portfolio)
    print(list(lst))
  2. 有下列三种数据类型,

     l1 = [1,2,3,4,5,6]
     l2 = ['oldboy','alex','wusir','太白','日天']
     tu = ('**','***','****','*******')

    写代码,最终得到的是(每个元祖第一个元素>2,第三个*至少是4个。)

     [(3, 'wusir', '****'), (4, '太白', '*******')]

    这样的数据。

    l1 = [1, 2, 3, 4, 5, 6]
    l2 = ['oldboy', 'alex', 'wusir', '太白', '日天']
    tu = ('**', '***', '****', '*******')
    lst = zip(l1,l2,tu)
    print(list(filter(lambda x:x[0]>2 and len(x[2])>3,list(lst))))
  3. )有如下数据类型(实战题):

     l1 = [ {'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}]

    将l1按照列表中的每个字典的values大小进行排序,形成一个新的列表。

    l1 = [{'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}]
    lst = sorted(l1,key=lambda x:x["sales_volumn"])
    print(lst)
  4. 有如下数据结构,通过过滤掉年龄大于16岁的字典

    lst = [{'id':1,'name':'alex','age':18},
            {'id':1,'name':'wusir','age':17},
            {'id':1,'name':'taibai','age':16},]
lst = [{'id':1,'name':'alex','age':18},
        {'id':1,'name':'wusir','age':17},
        {'id':1,'name':'taibai','age':16},]
new_lst =filter(lambda x:x["age"]>16,lst)
print(list(new_lst))

9.有如下列表,按照元素的长度进行升序

lst = ['天龙八部','西游记','红楼梦','三国演义']
lst = ['天龙八部','西游记','红楼梦','三国演义']
new_lst = sorted(lst,key=lambda x:len(x))
print(new_lst)

10.有如下数据,按照元素的年龄进行升序

lst = [{'id':1,'name':'alex','age':18},
    {'id':2,'name':'wusir','age':17},
    {'id':3,'name':'taibai','age':16},]
lst = [{'id':1,'name':'alex','age':18},
    {'id':2,'name':'wusir','age':17},
    {'id':3,'name':'taibai','age':16},]
new_lst = sorted(lst,key=lambda x:x["age"])
print(new_lst)

11..看代码叙说,两种方式的区别

lst = [1,2,3,5,9,12,4]
lst.reverse()
print(lst)

print(list(reversed(lst)))
lst = [1,2,3,5,9,12,4]
lst.reverse()
print(lst)
#直接修改了原列表

print(list(reversed(lst)))
#原列表没有改变,只是新建了一个列表

求结果(面试题)

v = [lambda :x for x in range(10)]
print(v)
print(v[0])
print(v[0]())
v = [lambda :x for x in range(10)]
print(v)#10个lambda函数地址
print(v[0])#第一个lambda函数的地址<function <listcomp>.<lambda> at 0x000002846C5EDB70>
print(v[0]())#9

13.求结果(面试题)

v = (lambda :x for x in range(10))
print(v)
print(v[0])
print(v[0]())
print(next(v))
print(next(v)())
v = (lambda :x for x in range(10))
print(v)#生成器地址<generator object <genexpr> at 0x00000257F81E4C50>
print(v[0])#报错
print(v[0]())#报错
print(next(v))#生成器第一个函数的地址<function <genexpr>.<lambda> at 0x00000257F827DAE8>
print(next(v)())#1

14.map(str,[1,2,3,4,5,6,7,8,9])输出是什么? (面试题)

map(str,[1,2,3,4,5,6,7,8,9])
#什么也不输出
print(map(str,[1,2,3,4,5,6,7,8,9]))
#得到一个迭代器的地址
print(list(map(str,[1,2,3,4,5,6,7,8,9])))
#['1','2','3','4','5','6,'7','8','9']

15.有一个数组[34,1,2,5,6,6,5,4,3,3]请写一个函数,找出该数组中没有重复的数的总和(上面数据的么有重复的总和为1+2=3)(面试题)

#方法一

lst = [34,1,2,5,6,6,5,4,3,3]
l = 2*sum(set(lst)) - sum(lst)
print(l)
#方法二
lst = [34,1,2,5,6,6,5,4,3,3]
from functools import reduce
lst = sum(filter(lambda x: lst.count(x) < 2, lst))
print(lst)

16.求结果:(面试题,比较难,先做其他题)

def num():
    return [lambda x:i*x for i in range(4)]
print([m(2)for m in num()])
结果
#[6,6,6,6]

lambda map() filter() zip()练习

标签:tool   res   lenovo   列表   lam   字符串   cti   count   过滤   

原文地址:https://www.cnblogs.com/ciquankun/p/11227969.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!