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

习题二 答案

时间:2017-10-03 17:49:55      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:range   bsp   start   lex   alt   index   开头   code   nbsp   

查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素。

1 li = ["alec", " aric", "Alex", "Tony", "rain"]
2 tu = ("alec", " aric", "Alex", "Tony", "rain") 
3 dic = {‘k1‘: "alex", ‘k2‘: ‘ aric‘,  "k3": "Alex", "k4": "Tony"}
 1 li = ["alec", " aric", "Alex", "Tony", "rain"]
 2 tu = ("alec", " aric", "Alex", "Tony", "rain") 
 3 dic = {k1: "alex", k2:  Aric,  "k3": "Alex", "k4": "Tony"}
 4 
 5 for a in range(len(li)):
 6     li[a] = li[a].replace(" ", "")
 7 print(li)
 8 
 9 tu = list(tu)
10 for b in range(len(tu)):
11     tu[b] = tu[b].replace(" ", "")
12 tu = tuple(tu)
13 print(tu)
14 
15 for c in dic:
16     dic[c] = dic[c].replace(" ", "")
17 print(dic)
18 
19 for a1 in li:
20     if (a1.startswith("a") or a1.startswith("A")) and a1.endswith("c"):
21         print(a1)
22 
23 for b1 in tu:
24     if (b1.startswith("a") or b1.startswith("A")) and b1.endswith("c"):
25         print(b1)
26         
27 for c1 in dic:
28     c2 = dic[c1].strip()
29     if (c2.startswith("a") or c2.startswith("A")) and c2.endswith("c"):
30         print(c2)

输出结果:

[alec, aric, Alex, Tony, rain]
(alec, aric, Alex, Tony, rain)
{k3: Alex, k1: alex, k2: Aric, k4: Tony}
alec
aric
alec
aric
Aric

 

习题二 答案

标签:range   bsp   start   lex   alt   index   开头   code   nbsp   

原文地址:http://www.cnblogs.com/zhangzengqiang/p/7623987.html

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