zoukankan      html  css  js  c++  java
  • Python入门3

    字典

     字典四种创建方法:

    1、

    dic = dict()
    print(dic)
    # 相当于
    dic1 = {}
    print(dic1)
    
    # 输出
    {}
    {}

    2、

    dic = dict(one=1, two=2)
    print(dic)
    # 输出
    {'one': 1, 'two': 2}

    3、最常使用的定义字典方式

    dic1 = {'name': 'abc', 'age': 20}
    print(dic1)
    
    # 输出
    {'name': 'abc', 'age': 20}

    4、

    dic1 = dict((('name', 'abc'), ('age', 18)))
    print(dic1)
    
    # 输出
    {'age': 18, 'name': 'abc'}

    字典的copy(浅拷贝)

    acc1 = {'name': 'aaa', 'account': {'credit': 3000, 'balance': 3000}}
    acc2 = acc1.copy()
    acc2['name'] = 'ccc'
    print(acc1)
    print(acc2)

    输出:从输出可以看出,acc2的名字发生了改变,但是其它没有变化

    那我把acc2的列表里面内容改了呢?

    acc1 = {'name': 'aaa', 'account': {'credit': 3000, 'balance': 3000}}
    acc2 = acc1.copy()
    acc2['name'] = 'ccc'
    print('acc1_dic-->', acc1)
    print('acc2_dic-->', acc2)
    
    acc2['account']['balance'] = 2500
    print('acc1_dic-->', acc1)
    print('acc2_dic-->', acc2)
    
    print('acc1_dic-->', id(acc1['account']))
    print('acc2_dic-->', id(acc2['account']))

     从输出可以看出,更改了acc2列表里面的内容,acc1列表里面的也自动更改了,同时通过对比2个字典里面列表位置可以看出,都是引用一个内存地址。

    总结:

    通过以上例子说明,浅拷贝只在第一层数据改变时候生效,当有第二层数据时候,就是引用了。

    集合(set)

     1、去重功能

    li = [1, 2, 3, 4, 5, 6, 2, 4, 5, ]
    s1 = set(li)
    print(s1)
    
    # 输出
    {1, 2, 3, 4, 5, 6}

    2、交集:取2个集合内共有部分

    s1 = {1, 2, 3, 4, 5}
    s2 = {2, 3, 6, 7}
    
    print(s1 & s2)
    print(s1.intersection(s2))  # 另外一个写法
    
    # 输出
    {2, 3}
    {2, 3}

    3、并集:取2个集合内不同部分

    s1 = {1, 2, 3, 4, 5}
    s2 = {2, 3, 6}
    
    print(s1 | s2)
    print(s1.union(s2))  # 另外一个写法
    # 输出
    {1, 2, 3, 4, 5, 6}

    4、差集:属于集合A且不属于集合B

    s1 = {1, 2, 3, 4}
    s2 = {2, 3, 6, 8}
    
    print(s1 - s2)
    print(s1.difference(s2))  # 另外一个写法
    
    # 输出
    {1, 4}
    
    s1 = {1, 2, 3, 4}
    s2 = {2, 3, 6, 8}
    
    print(s2 - s1)
    print(s2.difference(s1))  # 另外一个写法
    
    # 输出
    {8, 6}

    5、对称差集:把2个交集内相同的去掉,取不同的内容

    s1 = {1, 2, 3, 4}
    s2 = {2, 3, 6, 8}
    
    print(s1 ^ s2)
    print(s1.symmetric_difference(s2))  # 另外一个写法
    
    # 输出
    {1, 4, 6, 8}

    6、子集:就是包含,谁包含谁,如果s1所有元素包含s2所有元素,那么s2就是s1的子集

    s1 = {1, 2, 3, 4, 5}
    s2 = {1, 2, 3}
    
    print(s1 <= s2)
    print(s1.issubset(s2))  # 另外一个写法
    
    print(s2.issubset(s1))
    
    # 输出
    False
    False
    True

    7、父集:跟子集一样

    s1 = {1, 2, 3, 4, 5}
    s2 = {1, 2, 3}
    
    print(s1 >= s2)
    print(s1.issuperset(s2))  # 另外一个写法
    
    # 输出
    True
    True

    文件操作(open)

    文件操作主要使用open 及 with open语句进行文件操作。文件的打开,主要包含三步:

    1、找到文件

    2、打开文件(是用什么模式)

    3、关闭文件

    文件打开模式:

    r: open for reading (default)

    w: open for writing,truncating the file first

    x: create a new file and open it for writing,if the file already exists,raises an `FileExistsError` (only python 3.x)

    a: open for writing, appending to the end of the file if it exists

    b: binary mode

    +: open a disk file for updating (reading and writing)

    r+: reading and writing 比较常用

    w+: opens and truncates the file to 0 bytes 没啥卵用

    a+: append

    带b和不带b的区别:

    1、读取上的区别,带b读取是字节,不带b读取是字符串。

    2、如果在模式里面带b,encoding这个参数也不能使用了,代表你告诉python,不再需要你帮我处理,我直接跟二进制打交道,也就意味这,你拿到的数据是2进制的,写的时候也必须是2进制才可以。

    so:你在写的时候需要把字符串转换为字节,才可以进行写入

    f = open('utf-8.txt', 'ab')
    f.write(bytes('', encoding='utf-8'))
    f.close()

    文件基本操作方法:

    通过TextIOWrapper类里面可以看见文件操作有哪些方法

    1、read: 在文件比较大的时候不推荐使用,

    这里的encoding编码意思是你告诉python你打开的时候是以什么编码方式打开,如果打开乱码,基本都是因为这里设置有问题造成的

    如果你的文件是以GBK保存的,这里就改为GBK,如果是用utf-8,这里就改为utf-8就对了

    read() 无参数:读取全部内容

        有参数:根据打开模式,如果有b,就按照字节读取,无b按照字符读取

    f = open('test', encoding='utf-8')
    data = f.read()
    print(data)
    f.close()

    2、write

    这个时候追加的信息会放到最后,那能不能调整写的位置呢?

    f = open('test', 'r+', encoding='utf-8')
    data = f.read()
    print(data)
    f.write('6666')
    f.close()
    
    # 输出
    1111
    2222
    3333
    4444
    5555
    中文

    3、seek: 调整指针位置(与打开模式没有关系,按照字节)

    seek永远是以字节的方式去寻找位置,这个千万记住了

    把888写到指针1的位置后边,并且覆盖了原来的111

    f = open('test', 'r+', encoding='utf-8')
    data = f.read()
    f.seek(1)
    print(data)
    f.write('888')
    f.close()
    
    # 输出
    1888
    2222
    3333
    4444
    5555
    中文

    4、tell: 获取当前指针位置(与打开模式没有关系,按照字节)

    test文件内容:

    陈1888

    f = open('test', 'r+', encoding='utf-8')
    data = f.read(1)
    print('--->', f.tell())
    print(data)
    f.close()
    
    # 输出
    ---> 3

    5、readline: 一行一行读

    f = open('test', 'r+', encoding='utf-8')
    print(f.readline().strip())
    print(f.readline().strip())
    
    f.close()

    6、truncate: 截断数据

    这个例子意思是:保留前5个字节内容,后边全部不要

    f = open('test', 'r+', encoding='utf-8')
    f.seek(5)  # 把指针调整到第5个位置
    f.truncate()
    f.close()

    7、readlines: 按行读取并返回列表

    f = open('test', 'r+', encoding='utf-8')
    
    data = f.readlines()
    print(data)

    8、最常用的使用循环一行一行读取文件

    f = open('test', 'r+', encoding='utf-8')
    
    for line in f:
        print(line.strip())

    9、flush:强制把内存的信息立刻写入的硬盘中

    文件操作(with open)

    1、with open 方式跟open的操作基本一样,是用with open 可以不用显性的是用f.close(),它自动进行关闭。

    2、with open 方式支持同时打开2个以上的文件,这个open是不可以的。

    with open('test') as f1 , open('test1') as f2:
        data1 = f1.read()
        data2 = f2.read()
        print(data1)
        print(data2)

    复制test文件前2行到test2文件

    with open('test') as f1, open('test2', 'x') as f2:
        count = 0
        for line in f1:
            if count < 2:
                f2.write(line)
                count += 1
            else:
                break
  • 相关阅读:
    Web前端开发资源集锦
    刮刮卡效果
    html5游戏驴子跳
    Bootstrap3实现的响应式幻灯滑动效果个人作品集/博客网站模板
    40款免费社交图标素材
    移动端的推拉效果导航菜单-支持响应式及其多层菜单
    通过HTML5 Visibility API检测页面活动状态
    HTML5游戏开发引擎Pixi.js新手入门讲解
    滚动触发的翻转式文字引用效果
    11个实用的CSS学习工具
  • 原文地址:https://www.cnblogs.com/chen1930/p/5989203.html
Copyright © 2011-2022 走看看