lambda map () filter () zip () Exercise

Exercise:

  1. To deal with the map string list, the list of all become sb, for example 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. With the following process to map l, then obtain a new list with the list, the list is the name of each of the end sb

l=[{'name':'alex'},{'name':'y'}]
l=[{'name':'alex'},{'name':'y'}]
l1 = list(map(lambda x:{"name":x["name"]+"sb"},l))
print(l1)
  1. With a filter to deal with, to get the stock price is greater than 20 stock names
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. There are the following dictionary obtain a total purchase price of each stock, and placed in an iterator.
    Result: list at [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. Or the above dictionaries, filtered by filter 100 is greater than the monovalent stock.

    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. There are the following three types of data,

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

    Write code, it is finally obtained (the first element of each ancestral> 2, and the third is at least 4 *.)

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

    Such data.

    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. ) Has the following types of data (actual title):

     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 will be sorted by size values ​​in the list for each dictionary, to form a new list.

    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. We have the following data structure, by filtering out older than 16 years old dictionary

    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. The following list, according to the length of elements in ascending

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

10. The following data, in ascending order according to age element

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)

Look at the code 11 .. narrative, the difference between the two ways

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)))
#原列表没有改变,只是新建了一个列表

Seeking results (interview questions)

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. The result of evaluating (interview questions)

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]) What is the output? (Interview questions)

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']

An array 15. [34,1,2,5,6,6,5,4,3,3] Please write a function to find the total number of the array does not repeat (the above data it has duplicate the sum of 1 + 2 = 3) (interview questions)

#方法一

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. The result of evaluating :( interview questions, more difficult, others do first title)

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

Guess you like

Origin www.cnblogs.com/ciquankun/p/11227969.html