day6

day6
1.判断下标为是否为偶数的方式来实现,如果为耦合下标就把值累加
方法1
a = [1,2,1,2,3,3,3,3]
result = 0
for i in a[::2]:
result += i
方法2:
for i in range(len(a)):
if i%2 == 0:
result += a[i]
 
2.倒叙取出每个单词的第一个字母
>>> s = "i am a boy"
>>> s_list = s.split()
>>> s_list
['i', 'am', 'a', 'boy']
>>> for i in s_list:
... print(i[0])
 
3.找出 s=”aabbccddxxxxffff”中,出现次数最多的字母
result = {}
for i in s:
if i in result:
result[i] += 1
else:
result[i] = 1
 
4.自定义count函数
1.只统计单个字符出现的次数
def count(s,letter):
num = 0
if not isinstace(s,str) and not isinstace(letter,str):
return 0
else:
for i in s:
if i == letter:
num += 1
return num
2.统计多个字符出现的次数
def counts(s,letter):
num = 0
letter_len = len(letter)
if not isinstance(s,str) and not isinstace(letter,str):
rerurn 0
elif letter not in s:
return 0
else:
for i in range(len(s)):
if s[i:i+letter_len] == letter:
num += 1
rerutn num
5自定义divmod
def divmod(a,b):
c = a//b
d = a%b
return c,d
 

posted @ 2019-12-23 08:15  绝世老中医  阅读(189)  评论(0编辑  收藏  举报