美文网首页Python五期爬虫作业
【Python爬虫】第一周练习(二)

【Python爬虫】第一周练习(二)

作者: Doggy米 | 来源:发表于2017-12-25 16:50 被阅读16次
    # -*- coding: utf-8 -*-
    
    """
    第四次 字典 json类型 练习题
    第五次 字符串分割、索引和切片练习题
    第六次 逻辑运算练习题
    """
    
    '''
    第四次 字典 json类型 练习题
    '''
    
    # 导入Json
    import json
    
    # 定义一个空字典dict_a,空字典dict_b
    dict_a = {}
    dict_b = {}
    
    # 给dict_a 添加3个key a1,a2,a3分别对应的值为b1,b2,b3
    dict_a["a1"] = "b1"
    dict_a["a2"] = "b2"
    dict_a["a3"] = "b3"
    
    # 获取dict_a所有的key,命名变量ks,打印输出ks及ks的数据类型
    ks = dict_a.keys()
    print("ks       ", ks)
    print("type(ks) ", type(ks))
    
    # 打印dict_a所有的value 命名变量vs,打印输出vs及vs的数据类型
    vs = dict_a.values()
    print("vs       ", vs)
    print("type(vs) ", type(vs))
    
    # 执行代码print(dict_a.items()) 观察输出结果
    print(dict_a.items())
    
    # 将a1和a3对应的值对换
    temp = dict_a["a1"]
    dict_a["a1"] = dict_a["a3"]
    dict_a["a3"] = temp
    
    # 打印输出dict_a
    print(dict_a)
    
    # 删除字典dict_a中a1对应的值
    dict_a.pop("a1")
    
    # 打印输出dict_a
    print(dict_a)
    
    # 将此时的dict_a数据更新到dict_b
    dict_b.update(dict_a)
    
    # 打印dict_b 并观察a1和a4是否在dict_b中
    print(dict_b)
    
    # a1如不存在dict_b中,输入以下代码 a1=dict_b.get('a1') 并打印变量a1
    a1 = dict_b.get('a1')
    print(a1)
    
    # 将13题变量a1 添加到dict_b中,key为'a1'
    dict_b["a1"] = a1
    
    # a4如不存在dict_b中,将a4对应的值默认为'null',并添加到dict_b中,key为'a4'
    a4 = 'null'
    dict_b["a4"] = a4
    
    # 打印dict_b及其数据类型
    print(dict_b, type(dict_b))
    
    # 将dict_b转化为json类型 命名为变量 json_c
    json_c = json.dumps(dict_b)
    
    # 打印json_c及其数据类型 观察16题打印结果和18题结果 将不同之处指明
    print(json_c, type(json_c))
    
    # 将json_c转换为字典类型 命名为dict_c 打印输出 dict_c及其数据类型
    dict_c = json.loads(json_c)
    print(dict_c, type(dict_c))
    
    '''
    第五次 字符串分割、索引和切片练习题
    '''
    
    # 理解索引这个会在之后经常用到
    # 定义字符串、例如:str1 = 'http://www.jianshu.com/u/a987b338c373'字符串内容为自己的首页连接
    str_1 = "https://www.jianshu.com/u/85c62640043d"
    
    # 输出自己的简书id(u/之后的内容--a987b338c373)
    print(str_1.split("u/")[1])
    
    # 设s = "abcdefg", 则下列值为
    # s[3]                s[2:4]
    # s[:5]               s[3:]
    # s[::-1]             s[::2]
    s = "abcdefg"
    print(s[3], "d")
    print(s[2:4], "cd")
    print(s[:5], "abcde")
    print(s[3:], "defg")
    print(s[::-1], "gfedcba")
    print(s[::2], "aceg")
    
    # 定义列表:list1 = [1,2,3,4,5,6,7],则下列值为
    # list1[3]            list1[2:4]
    # list1[:5]           list1[3:]
    # list1[::-1]         list1[::2]
    list1 = [1, 2, 3, 4, 5, 6, 7]
    print(list1[3], 4)
    print(list1[2:4], [3, 4])
    print(list1[:5], [1, 2, 3, 4, 5])
    print(list1[3:], [4, 5, 6, 7])
    print(list1[::-1], [7, 6, 5, 4, 3, 2, 1])
    print(list1[::2], [1, 3, 5, 7])
    
    # 定义元组:touple1 = (1,2,3,4,5,6,7),则下列值为
    # touple1[3]          touple1[2:4]
    # touple1[:5]         touple1[3:]
    # touple1[::-1]       touple1[::2]
    touple1 = (1, 2, 3, 4, 5, 6, 7)
    print(touple1[3], 4)
    print(touple1[2:4], (3, 4))
    print(touple1[:5], (1, 2, 3, 4, 5))
    print(touple1[3:], (4, 5, 6, 7))
    print(touple1[::-1], (7, 6, 5, 4, 3, 2, 1))
    print(touple1[::2], (1, 3, 5, 7))
    
    # 对之前学习过得集中基本类型及其方法进行复习,重中之重理解索引和切片
    
    '''
    第六次 逻辑运算练习题
    '''
    
    # 下列表达式逻辑运算后的结果为?(尽量直接思考解答,可以用代码测试结果)
    print("True and True", True and True, True)
    print("False and True", False and True, False)
    print("1 == 1 and 2 == 1", 1 == 1 and 2 == 1, False)
    print("\"test\" == \"test\"", "test" == "test", True)
    print("1 == 1 or 2 != 1", 1 == 1 or 2 != 1, True)
    print("True and 1 == 1", True and 1 == 1, True)
    print("False and 0 != 0", False and 0 != 0, False)
    print("True or 1 == 1", True or 1 == 1, True)
    print("\"test\" == \"testing\"", "test" == "testing", False)
    print("1 != 0 and 2 == 1", 1 != 0 and 2 == 1, False)
    print("\"test\" != \"testing\"", "test" != "testing", True)
    print("\"test\" == 1", "test" != "testing", True)
    print("not (True and False)", not (True and False), True)
    print("not (1 == 1 and 0 != 1)", not (1 == 1 and 0 != 1), False)
    print("not (10 == 1 or 1000 == 1000)", not (10 == 1 or 1000 == 1000), False)
    print("not (1 != 10 or 3 == 4)", not (1 != 10 or 3 == 4), True)
    print("not (\"testing\" == \"testing\" and \"Zed\" == \"Cool Guy\")",
          not ("testing" == "testing" and "Zed" == "Cool Guy"), True)
    print("1 == 1 and not (\"testing\" == 1 or 1 == 0)", 1 == 1 and not ("testing" == 1 or 1 == 0), True)
    print("3 == 3 and not (\"testing\" == \"testing\" or \"Python\" == \"Fun\")",
          3 == 3 and not ("testing" == "testing" or "Python" == "Fun"), False)
    

    相关文章

      网友评论

        本文标题:【Python爬虫】第一周练习(二)

        本文链接:https://www.haomeiwen.com/subject/rwnlgxtx.html