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

列表中的查找与筛选

时间:2016-08-03 18:22:06      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

本文主要是实现:查找列表中元素,移除每个元素的空格,并查找以 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_i1
tu = ("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_i2
dic = {‘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

运行结果:

技术分享

 

列表中的查找与筛选

标签:

原文地址:http://www.cnblogs.com/pythonlh/p/5733912.html

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