zoukankan      html  css  js  c++  java
  • Python 学习---------Day3

    第七章 字符串
    单双引号字符串是一样的
    用转义序列代表特殊字节
    字符串抑制转义
    myfile=open(r'C: ew ext.dat','w')
    三重引号编写多行字符串块
    字符串更大的编码集
    std(u'spam')
    unicode('spam')
    实际中的字符串
    基本操作
    索引和分片
    拓展分片:第三个限制值
    s[1:10:2]
    s{::2]
    s{::-1]
    字符串转换工具
    int('123'),str(42)
    repr(42)
    字符串代码转换
    ord('s') 115
    chr(115) s
    二进制的转换
    B='1101'
    I=0
    while B:
    I=I*2+ord(B[0]-ord('0'))
    B=B[1:]

    I=13

    'That is %d %s bird!' %(1,'dead')
    字符串格式化
    任何对象都可以转化为字符串,(打印时候使用),只需要使用%s这个代码来格式化表达式
    基于字典的字符串格式化
    "%(n)d %(x)s" %{"n":1,"x":spam}
    reply="""
    Greeting...
    Hello %(name)s!
    Your age is %(age)s
    """
    value={"name":"bob","age":12}
    print reply %value

    字符串方法
    s='xxxxSPAMxxxSPAMxxx'
    where=s.find("SPAM")
    s=s[:where]+'adas'+s[(where+4):]

    s.replace("spam",'asda')
    s.replace("soam","asda",1)

    s='spam'
    L=list(s)
    L[2]='z'
    s="".join(L) join方法可以把列表字符串连在一起
    split()方法可以将一个字符串分割成一个子字符串的列表
    line='aaa bbb ccc'
    cols=line.split() cols=['aaa','bbb','ccc']
    line="i'mSPAMSPAMlumberjack"
    line.split("SPAM")
    ["i'm", 'lumberjack']
    rstrip()
    upper()
    isalpha()
    endswith()

    第八章 列表与字典
    列表:
    任意对象的有序集合
    通过偏移读取
    可变长度,异构以及任意嵌套
    属于可变序列的分类
    对象引用数组
    基本列表操作
    len([1,2,3])
    [1,2,3]+[4,5,6]
    ['NI']*4
    3 in [1,2,3]
    for x in [1,2,3]:
    print x,
    str([1,2])+"34" '[1,2]34'
    [1,2]+list("34")
    原处修改列表
    L=['spam','Soan','SPAM"]
    L[1]='eggs'
    L=[0:2]=['eat','more']
    L.append('please')
    L.sort()
    append()和sort()之类的属性,对象的修改有点类似副作用,所以没必要重新赋值
    L[:0]=[x]
    L=[1,2]
    L.extend([3,4,5])
    L.pop()
    L.reverse()
    L=[]
    L.append(1)
    L.append(2)
    L.pop()
    del L[0]
    L[1:]=[]
    L[0]=[] L=[[]]
    字典
    通过键而不是偏移量来读取
    任意对象的无序集合
    可变长,异构,任意嵌套
    属于可变映射类型
    对象引用表(哈希表)

    字典的基本操作
    d={'spam':2,'ham':1,'eggs':3}
    d['spam']
    len(d)
    d.has_key('spam')
    d.keys()
    d['spam']=['grill','bake','fry']
    del d['eggs']
    d.values()
    d.items()
    d.get('spam')
    d2={'a':4,'b':5}
    d.uodate(d2)
    d
    d.pop('spam')
    for key in D.keys()
    for key in D
    字典用于稀疏数据结构
    matrix={}
    matrix[(2,3,4)]=88
    matrix[(1,2,3)]=89
    if else
    try :
    except keyError:
    matrix.get((2,3,4),0)

    dict.fromkeys(['a','b'],0)

    第九章 元组,文件以及其他
    元组
    任意对象的有序集合
    通过偏移存取
    属于不可变序列类型
    固定长度,异构,任意嵌套
    对象引用的数组
    元组的特殊语法:逗号和圆括号
    x=(40) x=40
    x=(40,) x=(40)
    如果你想对元组进行排序,先把它转换为列表,将它变为一个可变对象,然后再转换为元组
    t=('cc','aa','bb','dd')
    tmp=list(t)
    tmp.sort()
    t=tuple(tmp)

    注意元组的不可变性只适合用于元组本身顶层而并非其内容,例如元组内部的列表使可以像往常那样修改
    t=(1,2,[3,4],4)
    t[2][0]='spam'

    文件
    实际应用中的文件
    myfile=open('myfile','w')
    myfile.write('hello text file ')
    myfile.close()

    myfile=open('myfile')
    myfile.readline()
    myfile.readline()
    pickle 的使用
    struct 模块
    引用和拷贝
    copy方法可以复制字典,l[:]可以复制序列

    比较,相等性和真值
    == 操作符测试值的相等性
    is 操作符测试对象的一致性

    l1=[1,('a',3)]
    l2=[1,('a',3)]
    l1==l2 ,l1 is l2
    (True,False)

    s1='spam'
    s2='spam'
    s1==s2,s1 is s2
    (True,True)
    这里不一样的原因是在Python内部暂时存储并重复使用短字符串作为最佳化,事实上只有一个字符串'spam'供s1和s2使用
    如果使用长点的字符串
    s1='a long string'
    s2='a long string'
    s1==s2 s1 is s2
    (True,false)

  • 相关阅读:
    Hibernate一对多OnetoMany
    80端口被占用 PID = 4解决办法
    Devexpress Barmanager设置
    通过C# 打开一个应用程序
    C#DataTable操作
    C# Winform 双屏显示
    Dev gridview 调整字体大小
    在静态页面html中跳转传值
    js 求两个日期之间相差天数
    jQuery 知识积累
  • 原文地址:https://www.cnblogs.com/SqLver/p/4934092.html
Copyright © 2011-2022 走看看