2. 一个字符串中,分别输出奇数坐标字符或偶数坐标字符,奇数坐标的一行,偶数坐标的一行
str_a = "hello world"
#1
for i in range(len(str_a)):
if i %2 == 0:
print(str_a[i])
else:
print(str_a[i])
#2
s = range(len(str_a))
print([str_a[i] for i in list(filter(lambda x:x%2==0,s))])
print([str_a[i] for i in list(filter(lambda x:x%2==1,s))])
3. 统计字符串中的字母、数字、其他字符个数
#1
import string
str_a = "hello, 12 world!"
dic = {"int":0,"str":0,"te":0}
for i in str_a:
if i.isdigit():
dic["int"] += 1
if i in string.ascii_letters:
dic["str"] += 1
if i in string.punctuation:
dic["te"] += 1
print(dic)
4. 有一个已经排好序的列表。现输入一个数,要求按原来的规律将它插入列表中
#1
lst = [0,1,2,3,4,5,6,7,8,9]
def xun(x,n):
lst1 = x
for i in range(len(x)-1,0,-1):
if n >= x[i]:
lst.insert(x[i],n)
return lst1
xun(lst,5)
5. 统计名字列表中,各名字的首字母在名字列表中出现的次数
#1
lst = ["Allen","Mary","Sofia","Helen","Allen","Sofia"]
def name(x):
dic = {}
for i in x:
dic[str(i[0])] = "".join(x).count(i[0])
return dic
name(lst)