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