本文主要是实现:查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素。
li = ["alec", " aric", "Alex", "Tony", "rain"]
tu = ("alec", " aric", "Alex", "Tony", "rain")
dic = {‘k1‘: "alex", ‘k2‘: ‘ aric‘, "k3": "Alex", "k4": "Tony"}
代码:
#!/usr/bin/env python# -*- coding:utf-8 -*-# author by lh li = ["alec", " aric", "Alex", "Tony", "rain"]for i1 in li: new_i1=i1.strip() if (new_i1.startswith(‘a‘) or new_i1.startswith(‘A‘)) and new_i1.endswith(‘c‘): print new_i1tu = ("alec", " aric", "Alex", "Tony", "rain")for i2 in tu: new_i2=i2.strip() if (new_i2.startswith(‘a‘) or new_i2.startswith(‘A‘)) and new_i2.endswith(‘c‘): print new_i2dic = {‘k1‘: "alex", ‘k2‘: ‘ aric‘, "k3": "Alex", "k4": "Tony"}dict1=dic.values()for i3 in dict1: new_i3=i3.strip() if (new_i3.startswith(‘a‘) or new_i3.startswith(‘A‘)) and new_i3.endswith(‘c‘): print new_i3
运行结果:
时间: 2024-10-31 10:07:26