zoukankan      html  css  js  c++  java
  • Python课程第五天作业

    1、利用字典推导式和列表推导式完成数据的相互转化:

    dic = {'name': 'Owen', 'age': 18, 'gender': '男'}
    ls = [('name', 'Owen'), ('age', 18), ('gender', '男')]
    
    dic=[(k,v) for k,v in dic.items()]
    ls={k: v for k, v in ls}
    

    2、用生成器实现可以无限取值的生成器对象,第一次取值得到1,第二次取值得到3,第三次取值得到6,第四次取值得到10,依次类推

    def fn2():
        total = 0
        count = 1
        while True:
            total += count
            yield total
            count += 1
    obj = fn2()
    print(obj.__next__())
    print(obj.__next__())
    print(obj.__next__())
    print(obj.__next__())
    

    3、利用递归完成斐波那契数列案例:1、1、2、3、5、8、13、21初始1只兔子,1个月成熟,次月往后每月可以生出1只,每只兔子成熟后都具有繁殖能力,问一年半后一共有多少只兔子

    def func(depth,k1,k2):
        if depth > 18:
            return  k1
        k3=k1+k2
        res=func(depth+1,k2,k3)
        return res
    ret=func(1,0,1)
    print(ret)  

    4、利用map函数完成成绩映射 [58, 78, 98] => ['不通过', '通过', '通过'] 用到匿名函数,匿名函数的返回值用三元运算符处理

    a=map(lambda x: str(x) + '通过' if x > 60 else  str(x) + '不通过' ,[58, 78, 98])
    for i in a:
        print(i)

    5、利用reduce求[58, 78, 98]的总成绩

    from functools import reduce
    res=reduce(lambda a,b: a + b, [58, 78, 98])
    print(res)

    6、打印数字128的二进制、八进制、十六进制数

    print(bin(128))
    print(oct(128))
    print(hex(128))

    7、解决第7题求总成绩还可以用什么内置函数,输出13的7次幂的结果用什么内置函数,判断一个变量是否是函数对象的内置函数,原义字符串的内置函数

    print(sum([58, 78, 98]))
    print(pow(13,7))
    callable(func) 

    8.写出for迭代器迭代字符串、列表、元组、集合、字典(三种情况)的代码实现

    # Pyhon列表
    list_a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    # Pyhon元组
    tuple_a = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
    # Python字典
    dict_a = {'a': 1, 'b': 2, 'c': 3}
    
    # 可迭代对象的父类
    from collections import Iterable
    # 判断列表、元组、字典是否是可迭代对象
    print(isinstance(list_a, Iterable),
    isinstance(tuple_a, Iterable),
    isinstance(dict_a, Iterable))
    # 迭代列表
    for i in list_a:
        print(i)
    # 迭代列表,顺带获得索引了
    for i, v in enumerate(list_a):
        print(i, v)
    # 迭代字典中的值
    for value in dict_a.values():
        print(value)
    # 迭代字典中的键和值
    for k, v in dict_a.items():
        print(k, v)
    # 迭代由元组作为元素组成的列表
    for x, y in [(1, 1), (2, 4), (3, 9)]:
        print(x, y)

    9、用生成器完成自定义的my_range方法,可以实现和range类似的功能:

    my_range(5) => 能迭代出0,1,2,3,4
    my_range(5, 10) => 能迭代出5,6,7,8,9
    my_range(5, 10,2) => 能迭代出5,7,9
    my_range(10, 5, -2) => 能迭代出10, 8, 6
    my_range(10, 5) => 没结果
    my_range(5, 10, -1) => 没结果  

    代码:

    def my_range(start,end=0,step=1):
      count=0
      if end == 0 and step == 1:
          while count < start:
              yield count
              count += 1
      elif (end > 0 and end > start) and step == 1:
          while start < end:
              yield start
              start += 1
      elif (end > 0 and end > start) and step > 1:
          while start < end:
              yield start
              start += step
      elif (end > 0 and end < start) and step < 1:
          while end < start:
              yield start
              start += step
      elif (end > 0 and end < start) and step > 1:
          pass
      elif (end > 0 and end > start) and step < 1:
          pass
    
    
    for i in my_range(10,5,-2):
        print(i)

     

  • 相关阅读:
    P1227 【[JSOI2008]完美的对称】
    Hive使用Calcite CBO优化流程及SQL优化实战
    深入浅出Calcite与SQL CBO(Cost-Based Optimizer)优化
    神奇的传送门
    怎么设计一个秒杀系统
    我的收藏
    Redis 客户端 Jedis、lettuce 和 Redisson 对比
    Redis 的完整安装过程
    Redis GEO 功能使用场景
    Redis 传送门
  • 原文地址:https://www.cnblogs.com/panwenbin-logs/p/10792250.html
Copyright © 2011-2022 走看看