首页 > 代码库 > #查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素

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

 1 #!/usr/bin/env python
 2 #查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素。
 3 #    li = ["alec", " aric", "Alex", "Tony", "rain"]
 4 #    tu = ("alec", " aric", "Alex", "Tony", "rain") 
 5 #    dic = {‘k1‘: "alex", ‘k2‘: ‘ aric‘,  "k3": "Alex  ", "k4": "Tony"}
 6 
 7 li = ["   alex ","aric","Alex","Tony","rain"]
 8 tu = ("alex   ","aric ","Alex ","  Tony"," rain")
 9 dic = {k1:"alex",k2:" aric   ",k3:"Alex",k4:"Tony"}
10 for i,j in enumerate(li,0) :
11     j = j.strip()
12     li[i] = j
13     if j.endswith(c) and (j.startswith(a) or j.startswith(A)):
14         print(j)
15 print(li)
16 print("-------------------")
17 
18 
19 for j in tu :
20     print(j)
21     j = j.strip()
22     print(j)
23     if j.endswith(c) and (j.startswith(a) or j.startswith(A)):
24         print(j)
25 print(tu)
26 print("-------------------")
27 
28 
29 for i,j in enumerate(dic,0) :
30     print(j)
31     dic[j] = dic[j].strip()
32     if dic[j].endswith(c) and (dic[j].startswith(a) or dic[j].startswith(A)):
33         print(dic[j])
34 print(dic)
35 
36 for i in dic.values() :
37     i = i.strip()
38     if i.endswith(c) and (i.startswith(a) or i.startswith(A)):
39         print(dic[j])
40 print(dic)

 

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