9月27日作业
# 初学者定义函数的基本流程
# 第一步:确定函数功能
# 第二步:根据函数功能确定函数名
# 第三步:确定形参(看实现函数的功能需不需要额外的数据)
# 第四步:实现函数的功能(将形参当成对应的数据来使用)
# 第五步:确定函数返回值
# 第六步:写函数说明文档
# 1. 编写一个函数,交换指定字典的key和value。例如:dict1={'a':1, 'b':2, 'c':3} --> dict1={1:'a', 2:'b', 3:'c'}
def swap_key_value(dict1):
"""
交换指定字典的键和值(功能说明区)
:param dict1: 一个字典(参数说明区)
:return:None(返回值说明)
"""
new_dict={dict1[x]: x for x in dict1}
return new_dict
print(swap_key_value({'a': 10, 'b': 20}))
# 2. 编写一个函数,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串
def get_alphabet(str1):
"""
打印字符串中字母
:param str1: 一个字符串
:return: None
"""
new_str1=''.join(x for x in str1 if('a'<=x<='z'or 'A'<=x<='Z'))
return new_str1
str1='225gwet+'
print(get_alphabet(str1))
# 3. 写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母
def capitalize_(str1):
"""
将第一个字符是小写字母的字符串首字母变成大写字母
:param str1: 字符串
:return: None
"""
first = str1[0]
if first.islower:
result=first.upper()+str1[1:]
return result
else:
return str1
print(capitalize_('sdsg1335'))
# 4. 写一个自己的endswith函数,判断一个字符串是否已指定的字符串结束
def endswith_(str1, str2):
"""
判断一个字符串是否以指定的字符串结束
:param str1:指定字符串1
:param str2: 指定字符串2
:return: None
"""
len1 = len(str1)
len2 = len(str2)
if len1==len2:
return True
else:
if str1[-len2:]==str2:
return True
return False
print(endswith_('i love you', 'ME'))
# 5. 写一个自己的isdigit函数,判断一个字符串是否是纯数字字符串
def isdigit_(str1):
"""
判断一个字符串是否是纯数字字符串
:param str1: 指定字符串
:return: None
"""
for x in str1:
if '1'<=x<='9':
return True
return False
print(isdigit_('335667'))
# 6. 写一个自己的upper函数,将一个字符串中所有的小写字母变成大写字母
def upper_(str1):
"""
将一个字符串中所有的小写字母变成大写字母
:param str1: 指定字符串
:return: None
"""
new_str1=''.join(chr(ord(x)-32) if 'a'<=x<='z' else x for x in str1)
return new_str1
print(upper_('dqwdqwf'))
# 7. 写一个自己的rjust函数,创建一个字符串的长度是指定长度,原字符串在新字符串中右对齐,剩下的部分用指定的字符填充
def rjust_(str1, lenth, fill_char):
"""
创建一个字符串的长度是指定长度,原字符串在新字符串中右对齐,剩下的部分用指定的字符填充
:param str1: 指定字符串
:param lenth: 指定宽度
:param fill_char: 指定填充字符
:return: None
"""
len1=len(str1)
n = lenth-len1
if n <= 0:
result = str1
else:
result=fill_char * n + str1
return result
print(rjust_('qeq', 9, '+'))
# 8. 写一个自己的index函数,统计指定列表中指定元素的所有下标,如果列表中没有指定元素返回-1
def index_(list1, word):
"""
统计指定列表中指定元素的所有下标,如果列表中没有指定元素返回-1
:param list1: 指定列表
:param word: 指定元素
:return: None
"""
result=','.join((str(i) for i in range(len(list1)) if list1[i]==word))
if not result:
result=-1
return result
print(index_([1, 5, 6, 5, 8, 'u', 'z'], 'z'))
# 9.写一个自己的len函数,统计指定序列中元素的个数
def len_(order):
count=0
for x in order:
count += 1
return count
print([1, 4, 6, 8, 9])
# 10. 写一个自己的max函数,获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
def max_(order):
"""
获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
:param order: 指定序列
:return: None
"""
if type(order)==dict:
order=list(order.values())
else:
order=list(order)
max1 = order[0]
for x in order:
if x>max1:
max1=x
return max1
print(max_([1, 4, 5, 6, 6, 7, 54758, 8, 856]))
# 11. 写一个函数实现自己in操作,判断指定序列中,指定的元素是否存在
def in_(order, word):
for x in range(len(order)):
if order[x]==word:
return True
return False
# 12.写一个自己的replace函数,将指定字符串中指定的旧字符串转换成指定的新字符串
def replace1(str1, old_str, new_str):
"""
将指定字符串中指定的旧字符串转换成指定的新字符串
:param str1: 指定字符串
:param old_str: 旧字符串
:param new_str: 新字符串
:return:None
"""
new_str1 = new_str.join(str1.split(old_str))
return new_str1
print(replace1(str1='how are you?and you', old_str='you', new_str='me'))