查找列表中元素,移除每个元素的空格,并查找以 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