码迷,mamicode.com
首页 > 编程语言 > 详细

Python练习题(基础知识练习题(三))

时间:2017-11-06 20:13:28      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:logs   知识   and   移除   str   for   tar   int   列表   

1、请用代码实现:利用下划线将列表的每一个元素拼接成字符串,li = [‘alex‘,‘eric‘,‘rain‘]

#!/usr/bin/env python
# -*- coding:utf-8 -*-

li = [‘alex‘,‘eric‘,‘rain‘]
v = ‘_‘.join(li)
print(v)


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

li = ["alec", " aric", "Alex", "Tony", "rain"]

for S in li:
S = S.strip()
prefix_a = S.startswith(‘a‘)
prefix_A = S.startswith(‘A‘)
suffix_c = S.endswith(‘c‘)
if prefix_a or prefix_A and suffix_c:
print(‘要查找的元素:%s‘ % S)

tu = ("alec", " aric", "Alex", "Tony", "rain")

for S in tu:
S = S.strip()
prefix_a = S.startswith(‘a‘)
prefix_A = S.startswith(‘A‘)
suffix_c = S.endswith(‘c‘)
if prefix_a or prefix_A and suffix_c:
print(‘要查找的元素:%s‘ % S)


dic = {‘k1‘: "alex", ‘k2‘: ‘ aric‘,"k3": "Alex", "k4": "Tony"}

for S in dic.values():
S = S.strip()
prefix_a = S.startswith(‘a‘)
prefix_A = S.startswith(‘A‘)
suffix_c = S.endswith(‘c‘)
if prefix_a or prefix_A and suffix_c:
print(‘要查找的元素:%s‘ % S)

http://www.cnblogs.com/lianzibing/p/6105287.html

Python练习题(基础知识练习题(三))

标签:logs   知识   and   移除   str   for   tar   int   列表   

原文地址:http://www.cnblogs.com/wushuaishuai/p/7794574.html

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