zoukankan      html  css  js  c++  java
  • 深浅copy和字符串细节方法


    copy

    a=[1,2,3]
    b=a
    id(a)
    55499272
    id(b)
    55499272

    id()就是查看内存地址,是不是同一个对象。

    c=a.copy()
    id(c)
    57940040

    可见copy()出来的不是一个对象。


    a=[1,2,3]
    b=a
    a[1]=99
    a
    [1, 99, 3]
    b
    [1, 99, 3]

    列表元素变了,其他也跟着变了。

    列表里的每一个元素都是独立的个体,是有自己的内存地址。

    id(a)
    58246344
    id(a[0])
    1347775968
    id(a[1])
    1347779104

    所以a=b,表示两个列表指向了同一些元素。同一个杯子。
    a的元素变了,b的元素也跟着变。

    b=a.copy()
    copy就相当于新起一个杯子,把元素都复制过去。

    注意:


    列表里有独立,也有一样?

    b=a.copy()
    a
    ['one', 2, ['triple', 4]]
    b
    ['1', 2, ['triple', 4]]
    id(a),id(b)
    (57026760, 56966024)
    id(a[0]),id(b[0])
    (59464760, 33713432)
    id(a[1]),id(b[1])
    (1701146112, 1701146112)
    id(a[2]),id(b[2])
    (59447944, 59447944)
    id(a[2][0]),id(b[2][0])
    (59465264, 59465264)


    b=a
    无copy,不独立,一改皆改

    b=a.copy()
    浅copy,第一层独立,第二层会跟着改变


    import copy

    a=[1,2,[3,4]]
    b=a
    c=a.copy()
    d=copy.deepcopy(a)

    a[0]='one'
    a[2][0]='three'

    print(a)
    print(b)
    print(c)
    print(d)

    ['one', 2, ['three', 4]]
    ['one', 2, ['three', 4]]
    [1, 2, ['three', 4]]
    [1, 2, [3, 4]]


    copy.deepcopy()是深拷贝,所有的层数都会跟着变

    ————————————————————————————————

    字符串

    字符串方法:
    s='Hello World'
    s2=s.swapcase()
    print(s2)

    大写变小写,小写变大写。
    按住ctrl,点击swapcase,就能看到方法的解释和类似源码
    def swapcase(self): # real signature unknown; restored from __doc__
    """
    S.swapcase() -> str

    Return a copy of S with uppercase characters converted to lowercase
    and vice versa.
    """
    return ""

    s1="i'll win CHAMPION"

    s1.capitalize()
    "I'll win champion"

    s1.casefold()
    "i'll win champion"

    s1.center(50,'-')
    "----------------i'll win CHAMPION-----------------"

    s1.count('i')
    2

    s1.endswith('n')
    False
    s1.endswith('N')
    True

    s2='a b'
    print(s2)
    a b
    s2.expandtabs(16)
    'a b'

    s1.find('win')
    5

    s3='{0} will win {1}'
    s3
    '{0} will win {1}'
    s3.format('you','second')
    'you will win second'

    s4='{who} will win {what}'
    s4.format(who='I',what='champion')
    'I will win champion'

    s1
    "i'll win CHAMPION"
    s1.index('win')
    5
    s1.index('win',2,11)
    5

    '33ddx'.isalnum()
    True
    '33ddx'.isalpha()
    False

    '3344'.isdecimal()
    True
    '3.344'.isdecimal()
    False

    '3344'.isdigit()
    True
    '3.344'.isdigit()
    False

    用isdigit()

    '_3a'.isidentifier()
    True
    '3a'.isidentifier()
    False
    是否一个可用的变量名

    'I am'.islower()
    False
    '7j'.islower()
    True

    '911.111'.isnumeric()
    False
    '911'.isnumeric()
    True
    是否只有数字

    'dd'.isprintable()
    True

    ' '.isspace()
    True
    ' '.isspace()
    True

    'Main road'.istitle()
    False
    'Main Road'.istitle()
    True

    'MAIN'.isupper()
    True

    ','.join('abc')
    'a,b,c'
    '-'.join(['abc','123','zzz'])
    'abc-123-zzz'

    'abc'.ljust(10,'*')
    'abc*******'
    len('abc'.ljust(10,'*'))
    10
    'abc'.rjust(10,'-')
    '-------abc'


    s1
    "i'll win CHAMPION"
    s1.lower()
    "i'll win champion"
    s1.upper()
    "I'LL WIN CHAMPION"


    s=' word str '
    s.strip()
    'word str'
    s.lstrip()
    'word str '
    s.rstrip()
    ' word str'


    做转换的:
    str1='abcde'
    str2='12345'
    table1=str.maketrans(str1,str2)
    table2=str.maketrans(str2,str1)
    str1.translate(table1)
    '12345'
    str2.translate(table2)
    'abcde'

    s='abc*123'
    s.partition('*')
    ('abc', '*', '123')


    s='1122 abc cbaabc'
    s.replace('1','xx')
    'xxxx22 abc cbaabc'
    s.replace('c','9',1)
    '1122 ab9 cbaabc'


    s='hello world'
    s.rfind('o')
    7


    s.rindex('o')
    7
    s.rindex('dd')
    Traceback (most recent call last):
    File "<input>", line 1, in <module>
    ValueError: substring not found


    s.rpartition('o')
    ('hello w', 'o', 'rld')


    s
    'hello world'
    s.split()
    ['hello', 'world']
    s.split('o')
    ['hell', ' w', 'rld']
    s.split('o',1)
    ['hell', ' world']

    split是把字符串变成列表

    s.rsplit()
    ['hello', 'world']
    s.rsplit('o')
    ['hell', ' w', 'rld']
    s.rsplit('o',1)
    ['hello w', 'rld']


    s2='a bc 123'
    s2.splitlines()
    ['a', 'bc', '123']


    s
    'hello world'
    s.startswith('hel')
    True
    s.endswith('llo')
    False
    s.endswith('ld')
    True


    s='Hello World'
    s.swapcase()
    'hELLO wORLD'


    s='abc uvw'
    s.title()
    'Abc Uvw'


    s='101011'
    s.zfill(20)
    '00000000000000101011'

    isdigit
    replace
    find
    count
    index
    strip
    center
    split
    format
    join

    ——————————————————————————————————————


    t1
    (1, 2, 3, ['a', 'b', 'c'], 3, 1)
    t1.count(1)
    2
    t1.index(2)
    1
    t1.index(['a','b','c'])
    3
    t1[1:-2]
    (2, 3, ['a', 'b', 'c'])

    元组是只读列表,有序但不可变。
    但元组里的列表是可变的,因为元组存储的是列表的地址

    wechat: nick753159 qq: 417966852 email: nzq42@qq.com base: shanghai
  • 相关阅读:
    linux资源监控命令详解
    c语言入门教程 / c语言入门经典书籍
    Hive存储过程实现-hpsql
    Hive集成mysql数据库
    Hive安装与配置
    Hbase shell操作总结(2)
    Hbase shell操作总结(1)
    Hbase 原理介绍
    Hbase的安装与配置
    Zookeeper工作原理(详细)
  • 原文地址:https://www.cnblogs.com/cyberbit/p/copy_str.html
Copyright © 2011-2022 走看看