码迷,mamicode.com
首页 > 其他好文 > 详细

s5_day5作业

时间:2017-06-27 15:00:00      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:put   函数   txt   列表   input   odi   pre   索引   value   

# 1、写函数,用户传入修改的文件名,与要修改的内容,执行函数,完成批量修改操作
# def number_file(file,change_s,change):
#     import os
#     with open(file, ‘r‘, encoding=‘utf-8‘)as read_f,open(‘z_file‘, ‘w‘, encoding=‘utf-8‘)as write_f:
#         for line in read_f:
#             if change_s in line:
#                 line = line.replace(change_s,change)
#             write_f.write(line)
#     os.remove(file)
#     os.rename(‘z_file‘, file)
# number_file(‘p.txt‘,‘123‘,‘789‘)
# 2、写函数,计算传入字符串中【数字】、【字母】、【空格] 以及 【其他】的个数
# def func(n):
#     num1=0
#     num2=0
#     num3=0
#     num4=0
#     for i in n:
#         if i.isdigit():
#             num1+=1
#         elif i.isspace():
#             num2+=1
#         elif i.isalpha():
#             num3+=1
#         else:
#             num4+=1
#     print(‘数字,%s 空格,%s 字母,%s 其他,%s‘%(num1,num2,num3,num4))
# func(input(‘请输入:‘))
# 3、写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5。
# def foo(in_put):
#     if len(in_put)>5:
#         print(in_put,‘长度大于5‘)
#     else:
#         print(in_put,‘长度小于等于5‘)
# foo((1,2,3,4,5,6))
# 4、写函数,检查用户传入的对象(字符串、列表、元组)的每一个元素是否含有空内容。
# def foo(in_put):
#     for i in in_put:
#         if i.isspace():
#             print(‘有空的输入‘)
#         else:
#             print(‘输入成功‘)
# foo(‘hello‘)
# 5、写函数,检查传入列表的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
# def foo(in_put):
#     a=[]
#     if len(in_put) >2:
#         a = in_put[:2]
#     else:
#         print(in_put)
#     return a
# print(foo([1,2,3,4,5,6]))
# 6、写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。
# def foo(n):
#     a=[]
#     for i in range(len(n)):
#         if i%2==1:
#             a.append(n[i])
#     return a
# print(foo([‘1‘,‘2‘,‘3‘,‘4‘]))
# 7、写函数,检查传入字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
#PS:字典中的value只能是字符串或列表
# dic = {"k1": "v1v1", "k2": [11,22,33,44]}
# def foo(dic):
#     for i in dic:
#         if len(dic[i])>2:
#             dic[i]=dic[i][:2]
#         else:
#             print(dic)
#     return dic
# print(foo(dic))

 

s5_day5作业

标签:put   函数   txt   列表   input   odi   pre   索引   value   

原文地址:http://www.cnblogs.com/z-x-y/p/7084731.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!