zoukankan      html  css  js  c++  java
  • day3

    字典

    定义

    {key1:value1,key2:value2},key-value结构,key必须可hash


    1.不可变:数字,字符串,元祖
    2.可变:列表,字典

    特性

    1. 比list占用内存多
    2. 无序
    3. key必须是唯一的,so 天生去重
    4. 查询速度比列表快
    为什么查询速度会快呢?因为他是hash类型,那为什么是hash呢
    
    哈希算法是将任意长度的二进制值映射为较短的固定长度的二进制值,这个小的二进制值成为哈希值。哈希值是一段数据唯一且极其紧凑的数值表示形式。如果散列将一段明文而且哪怕只更改该段落的一个字母,随后的哈希都将产生不同的值。要找到散列为同一个值的两个不同的输入,在计算上是不可能的,所以数据的哈希值可以检验数据的完整性。一般用于快速查找和加密算法
    
    
    dict会把所有的key变成hash表,然后将这个表进行排序,这样,你通过data[key]去查字典中一个key的时候,python会先把这个key hash成一个数字,然后拿这个数字到hash表中看没有这个数字,如果有,拿到这个key在hash表中的索引,拿到这个索引去与此key对应的value的内存地址那去取值就可以了

    字典的创建

    >>> person=dict(name='morgana',age=31)
    >>> person
    {'name': 'morgana', 'age': 31}
    >>> person={"name":"morgana","age":31}
    >>> person
    {'name': 'morgana', 'age': 31}
    >>> person = dict({"name":"morgana","age":31})
    >>> person
    {'name': 'morgana', 'age': 31}
    >>> 
    >>> person=dict((['name','morgana'],['age',31]))
    >>> person
    {'name': 'morgana', 'age': 31}
    
    >>> dic={}.fromkeys(['k1','k2'],[])
    >>> dic
    {'k1': [], 'k2': []}
    >>> dic={}.fromkeys(['k1','k2'],['a'])
    >>> dic
    {'k1': ['a'], 'k2': ['a']}
    >>> dic['k1'].append(1)
    >>> dic
    {'k1': ['a', 1], 'k2': ['a', 1]}
    >>> dic1={1:'morgana','name':'lhf',(1,2,3):{'age':18}}
    >>> dic1
    {1: 'morgana', 'name': 'lhf', (1, 2, 3): {'age': 18}}

    dic3=dict()
    dic4=dict(name='morgana',age=18)
    dic5=dict({'name':'morgana','age':18})
    dic6=dic((('name':'morgana'),('age':18),('gender':'male')))

    字典常用操作

    索引

    字典是无序的,所以不能通过索引来获取值,要通过键来找到关键值。对于不存在的键会出现KeyError

    获取字典里面的值

    查找

    >>> 'morgana' in dic1   #标准用法
    False
    >>> 1 in dic1         
    True
    >>> 'name' in dic1
    True


    >>> dic1={1:'morgana','name':'lhf',(1,2,3):{'age':18}} >>> dic1.get('name') #获取 'lhf' >>> dic1.get(1) 'morgana' >>> dic1.get('age') >>> dic1.get('1,2,3') >>> dic1[1] 同上 'morgana' >>> dic1['name'] 'lhf' >>> dic1[(1,2,3)] {'age': 18} >>> dic1.get('a') #如果一个key不存在,就报错,get不会,不存在只返回None >>> dic1['a'] Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'a'

    新增

    两种方法

    >>> dict1={}
    >>> dict1['a']=1
    >>> dict1
    {'a': 1}
    
    >>> dict1.setdefault('b',2)
    2
    >>> dict1
    {'b': 2, 'a': 1}

    删除Python字典

    #删除指定键
    >>> dict1
    {'b': 2, 'a': 1}
    >>> del dict1['a']
    >>> dict1
    {'b': 2}
    #清空字典
    >>> dict1.clear()
    >>> dict1
    {}
    
    #删除字典对象
    
    >>> del dict1
    >>> dict1    
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'dict1' is not defined

    >>> d1 = {'x':1,'y':3}
    >>> d2 = {'x':2,'z':1.4}
    >>> d1.update(d2)
    >>> d1
    {'y': 3, 'z': 1.4, 'x': 2}
    >>> 

    len( d )  返回字典d里面的键值对数目

    x in d   查询字典d中是否有键 x

    >>> d={'x':1,'y':2}
    >>> len(d)
    2
    >>> 'x' in d
    True
    >>> 'z' in d  
    False
    >>> 

    d [ x ] = y  若键 x 存在,则修改 x 对应的值为 y, 若键 x 不存在,则在字典 d 中增加键值对 x : y

    >>> d
    {'y': 2, 'x': 1}
    >>> d['u'] = 1.5
    >>> d
    {'u': 1.5, 'y': 2, 'x': 1}
    >>> d['u'] = 2.5
    >>> d
    {'u': 2.5, 'y': 2, 'x': 1}
    >>> 

    d.get( x [ , y]) 返回字典 d 中键 x 对应的值,键 x 不存在的时候返回 y, y 的默认值为None

    >>> d.get('x')
    1
    >>> del d['x'] 
    >>> d.get('x') 
    >>> d.get('x','nothing')
    'nothing'

    d.pop( x ) 返回给定键 x 对应的值,并将该键值对从字典中删除

    >>> d={'z':5,'x':1.5,'y':3}
    >>> d.pop('y')
    3
    >>> d
    {'x': 1.5, 'z': 5}
    >>> d.pop('y')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 'y'
    >>> 

    d.setdefault( x, [ , y ] )  返回字典 d 中键 x 对应的值,若键 x 不存在,则返回 y, 并将 x : y 作为键值对添加到字典中,y 的默认值为 None

    >>> d={'z':5,'x':1.5,'y':3}
    >>> d.setdefault('x')
    1.5
    >>> del d['x']
    >>> d.setdefault('x','look!')
    'look!'
    >>> d
    {'y': 3, 'x': 'look!', 'z': 5}
    >>> 

    循环dict

    info = {'stu1101':'morgana','stu1102':'katherine','stu1103':'haha'} 
    
    >>> for key in info:
    ...   print(key,info[key])
    ... 
    stu1102 katherine
    stu1101 morgana
    stu1103 haha

     

     

    集合

    定义

    集合是一个无序的,不重复的数据组合,它主要作用如下:

    • 去重,把一个列表变成集合,就自动去重了
    • 关系测试,测试两组数据之前的交集,差集,并集等关系

    集合的创建

    >>> {1,2,3,4}
    {1, 2, 3, 4}
    >>> 
    定义可变集合set
    >>> set_test=set('hello')
    >>> set_test
    {'l', 'e', 'h', 'o'}

    改为不可变集合frozenset
    >>> f_set_test=frozenset(set_test)
    >>> f_set_test
    frozenset({'l', 'e', 'h', 'o'})
    >>>

    常用操作

    #创建一个数值集合
    >>> s=set([3,5,9,10]) 
    #创建一个唯一字符的集合
    >>> t=set("hello")
    #t和s的并集
    >>> s=set([3,5,9,10]) 
    >>> t=set("hello")
    >>> a = s | t
    >>> a
    {3, 5, 'l', 'h', 9, 10, 'o', 'e'}
    #t和s的交集
    >>> b = s & t
    >>> b
    set()
    # 求差集(项在t中,但不在s中)
    >>> c = t - s
    >>> c
    {'h', 'o', 'e', 'l'}
    #对称差集(项在t或者s中,但不会同时出现的二者中)
    
    >>> d = t ^ s
    >>> d
    {3, 5, 'l', 'h', 9, 10, 'o', 'e'}
    
    基本操作
    
    >>> t.add('x')
    >>> s.update([10,37,42])
    >>> t
    {'l', 'x', 'e', 'h', 'o'}
    >>> s
    {3, 37, 5, 9, 10, 42}
    >>> 
    
    #使用remove删除一项
    >>> t.remove('h')
    >>> t
    {'l', 'x', 'e', 'o'}
    >>> 
    
    #测试x是否是s的成员
    >>> 'x' in s
    False
    
    #测试x是否不是s的成员
    >>> 'x' not in s
    True
    >>> 
    
    #测试是否s中的每一个元素都在t中 s <= t  
    >>> s.issubset(t)
    False
    >>> 
    
    #测试是否 t 中的每一个元素都在 s 中  s >= t  
    >>> s.issuperset(t)
    False
    >>>
    
    #返回一个新的set包含s和t中的每一个元素 s|t
    >>> s.union(t)
    {3, 42, 37, 5, 'l', 'e', 9, 10, 'o', 'x'}
    >>> 
    
    #返回一个新的set包含s和t的公共元素 s&t
    >>> s.intersection(t)
    set()
    >>> 
    
    #返回一个新的set包含s中但是t中没有的元素s - t 
    >>> s.difference(t)
    {3, 5, 37, 9, 10, 42}
    >>> 
    
    #返回一个新的set包含s和t都不重复的元素s ^ t
    >>> s.symmetric_difference(t) 
    {3, 37, 'l', 5, 9, 10, 42, 'o', 'e', 'x'}
    >>> 
    
    #返回set's'的一个浅复制
    >>> s.copy()
    {3, 42, 37, 5, 9, 10}
    >>> 

    更新

    >>> s1={1,2,3,'a','e'}
    >>> s1.update('e')
    >>> s1
    {'a', 2, 3, 'e', 1}
    >>> 

    增加

    >>> s1.add('hello')
    >>> s1
    {1, 2, 3, 'hello', 'a', 'e'}
    >>> 

    随机删除

    >>> s1
    {1, 2, 3, 'hello', 'a', 'e'}
    >>> s1.pop()
    1
    >>> s1
    {2, 3, 'hello', 'a', 'e'}
    >>> 

    指定删除

    >>> s1
    {2, 3, 'hello', 'a', 'e'}
    >>> s1.remove('a')
    >>> s1
    {2, 3, 'hello', 'e'}

    不报错删除

    >>> s1
    {2, 3, 'hello', 'e'}
    >>> s1.discard('a')
    >>> 

    >>> s1={1,2,3,'a','e'}
    >>> s2={1,2,3}
    >>> s1.difference_update(s2)
    >>> s1
    {'a', 'e'}
    >>>

    元祖

    只读列表,只有count,index 2个方法

    作用:如果一些数据不想被人修改,可以存成元祖,比如身份证列表

    >>> a=(('morgana','haha'))
    >>> a.count('morgana')
    1
    >>> a.index('morgana')
    0
    >>>

    字符编码

    先说python2

    1. py2里默认编码是ascii
    2. 文件开头那个编码声明是告诉解释这个代码的程序,是以什么编码格式把这段代码读入到内存,因为到了内存里,这段代码其实是以bytes二进制格式寸的,不过即使2进制流,也可以按不同的编码格式转成2进制流
    3. 如果在文件头声明了#_*_coding:utf-8_*_,就可以写中文了,不声明的话,python在处理这段代码时按ascii,显然会出错,记了这个声明后,里面的代码全是utf-8格式了
    4. 再有_*_ coding:utf-8_*_ 的情况下,你在声明变量如果写成name=u"金色",那这个字符就是unicode的格式,不加这个u,那你声明的字符串就是utf-8格式
    5. utf-8 to gbk 怎么转,utf-8先decode成unicode,然后在encode成gbk

    再说python3

    1. py3里面默认编码是utf-8,所以可以直接写成中文,也不需要文件头声明编码
    2. 你声明变量默认是unicode编码,不是utf-8编码,因为默认即时unicode,此时转gbk直接your_str.encode('gbk')
    3. 在py3里在你your_str.encode('gbk')时,就是encode的数据变成bytes里 可以理解为bytes就是2进制流,我们看到的不是0101,那是因为python为了让我们能对数据进行操作而在内存级别做了一层封装
    4. 在py2里也有bytes,但是py2里的bytes只是对str做了个别名

    文件操作

    对文件操作流程

    1. 打开文件,得到一个文件句柄并赋值给一个变量
    2. 通过句柄对文件进程操作
    3. 关闭文件
    Somehow, it seems the love I knew was always the most destructive kind
    不知为何,我经历的爱情总是最具毁灭性的的那种
    Yesterday when I was young
    昨日当我年少轻狂
    The taste of life was sweet
    生命的滋味是甜的
    As rain upon my tongue
    就如舌尖上的雨露
    I teased at life as if it were a foolish game
    我戏弄生命 视其为愚蠢的游戏
    The way the evening breeze
    就如夜晚的微风
    May tease the candle flame
    逗弄蜡烛的火苗
    The thousand dreams I dreamed
    我曾千万次梦见
    The splendid things I planned
    那些我计划的绚丽蓝图
    I always built to last on weak and shifting sand
    但我总是将之建筑在易逝的流沙上
    I lived by night and shunned the naked light of day
    我夜夜笙歌 逃避白昼赤裸的阳光
    And only now I see how the time ran away
    事到如今我才看清岁月是如何匆匆流逝
    Yesterday when I was young
    昨日当我年少轻狂
    So many lovely songs were waiting to be sung
    有那么多甜美的曲儿等我歌唱
    So many wild pleasures lay in store for me
    有那么多肆意的快乐等我享受
    And so much pain my eyes refused to see
    还有那么多痛苦 我的双眼却视而不见
    I ran so fast that time and youth at last ran out
    我飞快地奔走 最终时光与青春消逝殆尽
    I never stopped to think what life was all about
    我从未停下脚步去思考生命的意义
    And every conversation that I can now recall
    如今回想起的所有对话
    Concerned itself with me and nothing else at all
    除了和我相关的 什么都记不得了
    The game of love I played with arrogance and pride
    我用自负和傲慢玩着爱情的游戏
    And every flame I lit too quickly, quickly died
    所有我点燃的火焰都熄灭得太快
    The friends I made all somehow seemed to slip away
    所有我交的朋友似乎都不知不觉地离开了
    And only now I'm left alone to end the play, yeah
    只剩我一个人在台上来结束这场闹剧
    Oh, yesterday when I was young
    噢 昨日当我年少轻狂
    So many, many songs were waiting to be sung
    有那么那么多甜美的曲儿等我歌唱
    So many wild pleasures lay in store for me
    有那么多肆意的快乐等我享受
    And so much pain my eyes refused to see
    还有那么多痛苦 我的双眼却视而不见
    There are so many songs in me that won't be sung
    我有太多歌曲永远不会被唱起
    I feel the bitter taste of tears upon my tongue
    我尝到了舌尖泪水的苦涩滋味
    The time has come for me to pay for yesterday
    终于到了付出代价的时间 为了昨日
    When I was young
    当我年少轻狂

    基本操作

    >>> f = open('lyrics') #打开文件
    >>> first_line = f.readline()      
    #读取一行  
    >>> print ('first_line:',first_line)
    first_line: 不知为何,我经历的爱情总是最具毁灭性的的那种
    >>> print('我是分隔线'.center(50,'-'))
    ----------------------我是分隔线-----------------------
    
    #读取剩下的所有内容,文件大时不能用
    >>> data = f.read()
    #打印文件
    >>> print(data)
    Yesterday when I was young
    昨日当我年少轻狂
    The taste of life was sweet
    生命的滋味是甜的
    As rain upon my tongue
    就如舌尖上的雨露
    I teased at life as if it were a foolish game
    我戏弄生命 视其为愚蠢的游戏
    The way the evening breeze
    就如夜晚的微风
    May tease the candle flame
    逗弄蜡烛的火苗
    The thousand dreams I dreamed
    我曾千万次梦见
    The splendid things I planned
    那些我计划的绚丽蓝图
    I always built to last on weak and shifting sand
    但我总是将之建筑在易逝的流沙上
    I lived by night and shunned the naked light of day
    我夜夜笙歌 逃避白昼赤裸的阳光
    And only now I see how the time ran away
    事到如今我才看清岁月是如何匆匆流逝
    Yesterday when I was young
    昨日当我年少轻狂
    So many lovely songs were waiting to be sung
    有那么多甜美的曲儿等我歌唱
    So many wild pleasures lay in store for me
    有那么多肆意的快乐等我享受
    And so much pain my eyes refused to see
    还有那么多痛苦 我的双眼却视而不见
    I ran so fast that time and youth at last ran out
    我飞快地奔走 最终时光与青春消逝殆尽
    I never stopped to think what life was all about
    我从未停下脚步去思考生命的意义
    And every conversation that I can now recall
    如今回想起的所有对话
    Concerned itself with me and nothing else at all
    除了和我相关的 什么都记不得了
    The game of love I played with arrogance and pride
    我用自负和傲慢玩着爱情的游戏
    And every flame I lit too quickly, quickly died
    所有我点燃的火焰都熄灭得太快
    The friends I made all somehow seemed to slip away
    所有我交的朋友似乎都不知不觉地离开了
    And only now I'm left alone to end the play, yeah
    只剩我一个人在台上来结束这场闹剧
    Oh, yesterday when I was young
    噢 昨日当我年少轻狂
    So many, many songs were waiting to be sung
    有那么那么多甜美的曲儿等我歌唱
    So many wild pleasures lay in store for me
    有那么多肆意的快乐等我享受
    And so much pain my eyes refused to see
    还有那么多痛苦 我的双眼却视而不见
    There are so many songs in me that won't be sung
    我有太多歌曲永远不会被唱起
    I feel the bitter taste of tears upon my tongue
    我尝到了舌尖泪水的苦涩滋味
    The time has come for me to pay for yesterday
    终于到了付出代价的时间 为了昨日
    When I was young
    当我年少轻狂
    #关闭文件
    >>> f.close()

    打印前5行

    line_nu=0
    
    for line in f:
        if line_nu < 5: 
          print(line.strip())
          line_nu += 1
        else:
          break
    for i in range(5):
        for line in f:
         print(line.strip())
  • 相关阅读:
    【iCore4 双核心板_FPGA】例程八:乘法器实验——乘法器使用
    【iCore4 双核心板_ARM】例程十一:DMA实验——存储器到存储器的传输
    【iCore1S 双核心板_FPGA】例程八:触发器实验——触发器的使用
    【iCore4 双核心板_ARM】例程十:RTC实时时钟实验——显示时间和日期
    【iCore4 双核心板_ARM】例程九:ADC实验——电源监控
    WebBrowser的Cookie操作之流量刷新机
    网站受攻击的常用手段
    八爪鱼招标网的百度权重升为2了,独立IP也从0快速发展为1000
    网站添加数据出错,原来是MS SQL Server2008日志文件占据空间过大导致的
    当你的网站被疯狂攻击时你能做什么?
  • 原文地址:https://www.cnblogs.com/morgana/p/5994582.html
Copyright © 2011-2022 走看看