day9-作业

2018-11-15  本文已影响0人  2333_11f6

0.写一个匿名函数,判断指定的年是否是闰年
程序:

# 0.写一个匿名函数,判断指定的年是否是闰年
leap_year = lambda years: '是闰年' if (years % 4 == 0 and years % 100 != 0) \
                                   or years % 400 == 0 else '不是闰年'

print(leap_year(2000))
print(leap_year(2018))

结果:


运行结果

1.写一个函数将一个指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])(注意:不要使列表自带的逆序函数)
程序:

def reverse_list1(l:list):
    """输出列表l的逆序"""
    for index in range(len(l)//2):  
        l[index], l[len(l) - index - 1] = l[len(l) - index - 1], l[index]
    return l


list1 = [1, 2, 3]    
reverse_list1(list1)
print(list1)
list2 = [1, 2, 3, 4, 'y']
reverse_list1(list2)
print(list2)

结果:


运行结果

2.写一个函数,提取出字符串中所有奇数位上的字符
程序:

# 2.写一个函数,提取出字符串中所有奇数位上的字符
def extract(str1: str):
    """返回字符串str1所有奇数位上的字符"""
    str2 = str1[1::2]
    return str2


str1 = '012346789'
print(extract(str1))
str2 = 'abcdefghi'
print(extract(str2))

结果:


运行结果

3.写一个函数,统计指定列表中指定元素的个数(不用列表自带的count方法)
程序:

# 3.写一个函数,统计指定列表中指定元素的个数(不用列表自带的count方法)
def my_count(l: list, item1):
    """返回列表l中值为item1元素的个数"""
    count = 0
    for item in l:
        if item1 == item:
            count += 1
    return count


list1 = ['a', 'b', 1, 1, 1, 'c', 'j', 'c', 'j', 'c']
print('list1中c字符的个数为:', my_count(list1, 'c'))
print('list1中1的个数为:', my_count(list1, 1))

结果:


运行结果

4.写一个函数,获取指定列表中指定元素的下标(如果指定元素有多个,将每个元素的下标都返回)
例如: 列表是:[1, 3, 4, 1] ,元素是1, 返回:0,3
程序:

# 4.写一个函数,获取指定列表中指定元素的下标(如果指定元素有多个,将每个元素的下标都返回)
# 例如: 列表是:[1, 3, 4, 1] ,元素是1, 返回:0,3


def get_index(l: list, item):
    """获取列表l中元素值为item的元素下标"""
    index_l = []
    for index in range(len(l)):
        if item == l[index]:
            index_l.append(index)
    str1 = ''
    for item1 in index_l:
        str1 += str(item1)
    return ','.join(str1)


list1 = ['a', 'b', 1, 'a', 1, 'c', 'j', 'c', 'j', 'c']
print('list1中值为字符b的元素下标有:', get_index(list1, 'b'))
print('list1中值为1的元素下标有:', get_index(list1, 1))
print('list1中值为a的元素下标有:', get_index(list1, 'a'))

结果:


运行结果

5.写一个函数,能够将一个字典中的键值对添加到另外一个字典中(不使用字典自带的update方法)
程序:

# 写一个函数,能够将一个字典中的键值对添加到另外一个字典中(不使用字典自带的update方法)
def my_update(dict1: dict, dict2: dict):
    """将字典dict2中的键值对添加到字典dict1中,如果dict1中已有dict2中的key,
    则更新其对应的值。完成后打印出更新后的字典dict1"""
    for key2 in dict2:
        for key1 in dict1:
            if key1 == key2:
                dict1[key1] = dict2[key2]
        dict1[key2] = dict2[key2]

    print(dict1)


dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'c': 'j', 'd': 4, 'e': 5}
my_update(dict1, dict2)

结果:


运行结果

6.写一个函数,能够将指定字符串中的所有的小写字母转换成大写字母;所有的大写字母转换成小写字母(不能使用字符串相关方法)
程序:

# 6.写一个函数,能够将指定字符串中的所有的小写字母转换成大写字母;所有的大写字母
# 转换成小写字母(不能使用字符串相关方法)


def my_swapcase(str1: str):
    """将字符串str1中的所有的小写字母转换成大写字母;
    所有的大写字母转换成小写字母"""
    str2 = ''
    for item in str1:
        if 'a' <= item <= 'z':
            str2 += chr(ord(item)-32)
        elif 'A' <= item <= 'Z':
            str2 += chr(ord(item) + 32)
        else:
            str2 += item

    print(str2)


str1 = 'aha2312sduhdsASKDkddlad'
my_swapcase(str1)

结果:


运行结果

7.写一个函数,能够将指定字符串中的指定子串替换成指定的其他子串(不能使用字符串的replace方法)
例如: func1('abcdaxyz', 'a', '') - 返回: '\bcd\xyz'
程序:

# 7.写一个函数,能够将指定字符串中的指定子串替换成指定的其他子串(不能使用字符串
# 的replace方法)例如: func1('abcdaxyz', 'a', '\') - 返回: '\bcd\xyz'

# 1
def my_replace(str1: str, item1, item2):
    """将str1中值为item1的字符换为item2输出"""
    if len(item1) == 1:
        str2 = ''
        for item in str1:
            if item1 == item:
                str2 += item2
            else:
                str2 += item
    else:
        str2 = ''
        d = len(item1)
        index = 0
        while index < len(str1):
            if item1 == str1[index:index+d]:
                str2 += item2
                index += d
            else:
                str2 += str1[index]
                index += 1
    print(str2)


str1 = 'aha2312sahadduhdsahaASKDkddlahad'
my_replace(str1, 'ah', '\\')
my_replace(str1, 'aha', '\\')
my_replace(str1, 'a', '\\')

# 2
def my_replace(str1: str, item1, item2):
    """将str1中值为item1的字符换为item2输出"""
    str_list = str1.split(item1)
    strstr = str_list[0]
    for index in range(1,len(str_list)):
        strstr += item2 + str_list[index]

    return strstr


str1 = 'aha2312sduhdsASKDkddlad'
print(my_replace(str1, 'a', '\\'))

结果:


运行结果

8.实现一个输入自己的items方法,可以将自定的字典转换成列表。列表中的元素是小的列表,里面是key和value (不能使用字典的items方法)
例如:{'a':1, 'b':2} 转换成 [['a', 1], ['b', 2]
程序:

# 8.实现一个输入自己的items方法,可以将自定的字典转换成列表。列表中的元素是小的
# 列表,里面是key和value (不能使用字典的items方法)
# 例如:{'a':1, 'b':2} 转换成 [['a', 1], ['b', 2]


def my_items(dict1: dict):
    """将字典dict1转成列表。列表中的元素是小的
    列表,里面是key和value,并打印出改列表"""
    list1 = []
    i = 0
    for key in dict1:
        list2 = []
        list2.append(key)
        list2.append(dict1[key])
        list1.append(list2)
        list1[i] = list2
        i += 1

    print(list1)


my_items({'a': 1, 'b': 2})
my_items({'a': 1, 'b': 2, 'c': 3})

结果:


运行结果
上一篇下一篇

猜你喜欢

热点阅读