python每日练习

"""
习题 1:一个列表,排重,不能用 set,也不能用字典
"""
#方法一1:循环、遍历
l = [1,1,1,2,2,3,4,4,6]
new_l = []
for i in l:
    if i not in new_l:
        new_l.append(i)

print(new_l)

#方法2:set
l = [1,1,1,2,2,3,4,5,5]
result = list(set(l))
print(result)

#方法3:字典
l = [1,1,1,2,2,3,4,5,5]
result_d = {}
for i in l:
    if str(i) in result_d:
        result_d[str(i)] += 1
    else:
        result_d[str(i)] =1

print(result_d)

for k,v in result_d.items():
    print(k)

#方法4:通过删除index
s = [1,1,1,2,2,3,4,4]
t= s[1:]
for i in s:
    while t.count(i) > 1:
        del t[t.index(i)]

print(t)


"""
习题 2:判断一个输入的句子中有多少个字母
"""
s = 'I am a good girl!! 888~~~'
letters_num = 0
import string
for i in s:
    if i in string.ascii_letters:
        letters_num += 1

print(letters_num)

"""
习题 3、倒序取出每个单词的第一个字母。
"""
#方式 1:
s = "I am a good boy "
result = []
s_list = s.split()
print(s_list)
for word in s_list:
    result.append(word[0])

print(result[::-1])

#方法2:
s = "I am a good boy "
result = []
s_list = s.split()
for word in range(len(s_list)-1,-1,-1):
    result.append(s_list[word][0])


print(result)

"""
习题 4:找出 s=”aabbccddxxxxffff”中,出现次数最多 的字母
"""
#方法1:字典
s = "aabbccddxxxxffff"
res_d ={}
for i in s:
    if i in res_d:
        res_d[i] += 1
    else:
        res_d[i] = 1

print(res_d)
max_times = max(res_d.values())
for k,v in res_d.items():
    if v == max_times:
        print(k)


"""
习题 5、自定义 count 函数
"""
#只统计单个字符出现次数情况
s = 'aabbddkfrioss1211123'
def my_Count(s,letter):
    letter_count = 0
    if not isinstance(s,str) or not isinstance(letter,str):
        return 0
    else:
        for i in s:
            if i == letter:
                letter_count += 1
    return letter_count


print(my_Count(s,'a'))
print(my_Count(s,'1'))


#兼容统计多个字符出现次数的情况
#1、首先求出需要查找字符串的长度
#2、遍历源字符串,如果当前索引加上子串长度对应的字符串等于要查找字符串的话次数加
s = 'I am a good good good boy!!!  8888~~~'
s = 'I am a good good good boy!!!  8888~~~ am am a '
def myCount(s,word):
    word_num = 0
    length = len(word)
    if not isinstance(s,str) or not isinstance(word,str):
        return 0
    if word not in s:
        return 0
    else:
        for i in range(len(s)):
            if s[i:i+length] == word:
                word_num += 1
    return word_num

print(myCount(s,'good'))
print(myCount(s,'a'))


"""
习题6:自定义divmod函数
"""
def myDivmod(num1,num2):
    if not isinstance(num1,int) or not isinstance(num2,int):
        return 0
    a = num1 // num2
    b = num1 % num2
    return a,b

print(myDivmod(5,2))

"""
习题 7、把字符串中的所有数字去掉
"""
s = 'abc123fhler23998~~'
result = []
for i in s:
    if i not in '0123456789':
        result.append(i)


print(''.join(result))

#保留字符串中的字母
print(''.join(i for i in s if i.isalpha()))
print(''.join(filter(lambda x:x.isalpha(),s)))
#去掉字符串中数字
print(''.join(v for v in s if v not in '0123456789'))
print(''join(filter(lambda x:x not in '0123456789',s)))

"""
习题6:自定义divmod函数
"""
def myDivmod(num1,num2):
    if not isinstance(num1,int) or not isinstance(num2,int):
        return 0
    a = num1 // num2
    b = num1 % num2
    return a,b

print(myDivmod(5,2))

"""
习题 7、把字符串中的所有数字去掉
"""
s = 'abc123fhler23998~~'
result = []
for i in s:
    if i not in '0123456789':
        result.append(i)


print(''.join(result))

#保留字符串中的字母
print(''.join(i for i in s if i.isalpha()))
print(''.join(filter(lambda x:x.isalpha(),s)))
#去掉字符串中数字
print(''.join(v for v in s if v not in '0123456789'))
print(''.join(filter(lambda x:x not in '0123456789',s)))

"""
习题8:三个数排序
"""
def mySort(num1,num2,num3):
    if num1 > num2:
        num1,num2 = num2,num1
    if num1 > num3:
        num1,num3 = num3,num1
    if num2 > num3:
        num2,num3 = num3,num2
    return num1,num2,num3


print(mySort(2,6,1))

"""
习题9:求一个列表中的最大值
"""
def max(s):
    max_num = s[0]
    for i in s:
        if i > max_num:
            max_num = i
    return max_num

print(max([1,2,9,3]))

"""
习题10:求一个列表中的最大值、第二大的值
"""
def sec_num(s):
    max_num = s[0]
    sec_num = s[1]
    for i in s:
        if i > max_num:
            sec_num = max_num
            max_num = i
            #sec_num = max_num
        if max_num > i > sec_num:
            sec_num = i
    return sec_num

print(sec_num([1,2,8,10,4]))

 

转载于:https://www.cnblogs.com/ff-gaofeng/p/11264552.html

你可能感兴趣的:(python每日练习)