函数__内置函数、匿名函数(相关练习)

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

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

 

6. 用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,}
f1 = filter(lambda k: shares[k] > 20 , shares)
print(list(f1))

 

7.有下面字典,得到购买每只股票的总价格,并放在一个迭代器中

# 结果: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}]
def dic(dc):
    return dc['shares']*dc['price']
m = map(dic,portfolio)
print(list(m))

print(list(map(lambda a: a['shares']*a['price'],portfolio)))

 

8.#用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}]
print(list(filter(lambda a: a["price"]<100,portfolio)))

def dic(a):
    return a['price']<100

print(list(filter(dic,portfolio)))

 

9.有下列三种数据类型

l1 = [1,2,3,4,5,6]
l2 = ['oldboy','alex','wusir','太白','日天']
tu = ('**','***','****','*******')
# # 写代码,最终得到的是(每个元祖第一个元素>2,第三个*至少是4个)
# #     [(3, 'wusir', '****'), (4, '太白', '*******')]这样的数据

 

print(list(zip(l1, l2, tu)))
print(list(filter(lambda el: el[0]>2 and len(el[2])>=4, zip(l1, l2, tu))))

 

10.有如下数据类型:将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 d:d["sales_volumn"],reverse=True)
print(lst)

 冒泡排序的方法:(此答案的关键就是数据的位置交换,这里直接用=交换了)

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}]
for j in range(len(l1)):
    for i in range(len(l1)-1):
        if l1[i]['sales_volumn'] > l1[i + 1]['sales_volumn']:
            l1[i]['sales_volumn'] , l1[i + 1]['sales_volumn'] = l1[i + 1]['sales_volumn'],l1[i]['sales_volumn']
print(l1)

 

posted @ 2018-12-13 09:38  Tank-Li  阅读(162)  评论(0编辑  收藏  举报