#coding=utf-8#输入一句英文,要求倒叙打印出来。例如:i love you! ==>you! love i
b='i can go through any troubles, and you? \n ni hao '
list_a = b.split(' ')
while True:
if list_a[-1]=='':
list_a=list_a[:-1]
else:
break
print list_a
list_a.reverse()
print list_a
for i in list_a:
if '\n' == i:
print i
list_a.remove(i)
print ' '.join(list_a)
#输出字符串中出现次数最多的字符,输出起下标值(方法一)
str="abcdefabcdeffaaacccbffff123441f2233@#f#$!!123f"
dict={}
for i in str:
if dict.has_key(i):
dict[i]+=1
else:
dict[i]=1
print dict
values_sort=dict.values()
values_sort.sort()
most_times=values_sort[-1]
for i in dict.keys():
if dict[i]==most_times:
print i,'出现次数最多,总共出现了:%d次' % dict[i]
index=-1
print i, '的下标为:'
for j in str:
index+=1
if i==j:
print index,
print
#输出字符串中出现次数最多的字符,输出起下标值(方法一)
str="abcdefabcdeffaaacccbffff123441f2233@#f#$!!123f"
print str
str.count("f")
max_s=0
element=""
for s in str:
if str.count(s)>max_s:
max_s=str.count(s)
element=s
print u"出现次数最多的字符是:%s,共出现:%d次"%(element,str.count(element))
print u"出现的位置是:"
index=str.find(element)
while index!=-1:
print index,
index=str.find(element,index+1)
#统计名字列表中各名字首字母出险的次数(方法一)
names=['小红','小黄','大毛','大王','二蛋','小青']
def get_first_name():
#统计给出的名字列表中有多少姓,将其放入列表
first_names=[]#创建一个空的姓氏列表
for i in names:#遍历名字列表
first_name=i.decode('utf-8')[0]#取出名字列表中每个名字的姓氏
if first_name not in first_names:#判断取出的姓氏是否在姓氏列表
first_names.append(first_name)#如果姓氏不在列表中,将姓氏加入列表
return first_names#返回去重的姓氏列表
def numbers():
#计算每个姓氏有多少个
for i in get_first_name():
number = 0
for j in names:
f_n=j.decode("utf-8")[0]
if f_n==i:
number+=1
print i,'出现了:',number
numbers()
#统计名字列表中各名字首字母出险的次数(方法二)
dict={}for i in names: first_name=i.decode('utf-8')[0] if dict.has_key(first_name): dict[first_name]+=1else: dict[first_name]=1for i,j in dict.items(): print i,'出现了:',j,'次'