列表解析练习题

 1 '''
 2 有两个列表,分别存放来老男孩报名学习linux和python课程的学生名字
 3 linux=['钢弹','小壁虎','小虎比','alex','wupeiqi','yuanhao']
 4 python=['dragon','钢弹','zhejiangF4','小虎比']
 5 '''
 6 # # 问题一:得出既报名linux又报名python的学生列表
 7 
 8 ## 常规方法
 9 # linux=['钢弹','小壁虎','小虎比','alex','wupeiqi','yuanhao']
10 # python=['dragon','钢弹','zhejiangF4','小虎比']
11 # l=[]
12 # for i in linux:
13 #     for j in python:
14 #         if i==j:
15 #             l.append(i)
16 
17 ## 列表解析处理
18 # linux=['钢弹','小壁虎','小虎比','alex','wupeiqi','yuanhao']
19 # python=['dragon','钢弹','zhejiangF4','小虎比']
20 #
21 # l = [i for i in linux for j in python if i == j]
22 # print(l)
23 
24 # # 问题二:得出只报名linux,而没有报名python的学生列表
25 
26 # linux=['钢弹','小壁虎','小虎比','alex','wupeiqi','yuanhao']
27 # python=['dragon','钢弹','zhejiangF4','小虎比']
28 # l=[i for i in linux if i not in python]
29 # print(l)
30 
31 # 问题三:得出只报名python,而没有报名linux的学生列表
32 
33 '''
34 shares={
35     'IBM':36.6,
36     'lenovo':27.3,
37     'huawei':40.3,
38     'oldboy':3.2,
39     'ocean':20.1
40     }
41 '''
42 # # 问题一:得出股票价格大于30的股票名字列表
43 # shares={
44 #     'IBM':36.6,
45 #     'lenovo':27.3,
46 #     'huawei':40.3,
47 #     'oldboy':3.2,
48 #     'ocean':20.1
49 #     }
50 # s = [v for v in shares.values() if v > 30 ]
51 # print(s)
52 # # 问题二:求出所有股票的总价格
53 #
54 # l = [v for v in shares.values()]
55 # print(sum(l))
56 
57 '''
58 l=[10,2,3,4,5,6,7]
59 得到一个新列表l1,新列表中每个元素是l中对应每个元素值的平方
60 过滤出l1中大于40的值,然后求和
61 '''
62 # l=[10,2,3,4,5,6,7]
63 # l1 = [ i**2 for i in l ]
64 # 
65 # l2 = [i for i in l1 if i > 40]
66 # print(sum(l2))

 

posted @ 2018-04-11 22:49  JerryZao  阅读(442)  评论(0编辑  收藏  举报