How can I copy a Python string?

后端 未结 5 966
深忆病人
深忆病人 2020-12-12 23:38

I do this:

a = \'hello\'

And now I just want an independent copy of a:

import copy

b = str(a)
c = a[:]
d = a          


        
5条回答
  •  时光取名叫无心
    2020-12-13 00:05

    To put it a different way "id()" is not what you care about. You want to know if the variable name can be modified without harming the source variable name.

    >>> a = 'hello'                                                                                                                                                                                                                                                                                        
    >>> b = a[:]                                                                                                                                                                                                                                                                                           
    >>> c = a                                                                                                                                                                                                                                                                                              
    >>> b += ' world'                                                                                                                                                                                                                                                                                      
    >>> c += ', bye'                                                                                                                                                                                                                                                                                       
    >>> a                                                                                                                                                                                                                                                                                                  
    'hello'                                                                                                                                                                                                                                                                                                
    >>> b                                                                                                                                                                                                                                                                                                  
    'hello world'                                                                                                                                                                                                                                                                                          
    >>> c                                                                                                                                                                                                                                                                                                  
    'hello, bye'                                                                                                                                                                                                                                                                                           
    

    If you're used to C, then these are like pointer variables except you can't de-reference them to modify what they point at, but id() will tell you where they currently point.

    The problem for python programmers comes when you consider deeper structures like lists or dicts:

    >>> o={'a': 10}                                                                                                                                                                                                                                                                                        
    >>> x=o                                                                                                                                                                                                                                                                                                
    >>> y=o.copy()                                                                                                                                                                                                                                                                                         
    >>> x['a'] = 20                                                                                                                                                                                                                                                                                        
    >>> y['a'] = 30                                                                                                                                                                                                                                                                                        
    >>> o                                                                                                                                                                                                                                                                                                  
    {'a': 20}                                                                                                                                                                                                                                                                                              
    >>> x                                                                                                                                                                                                                                                                                                  
    {'a': 20}                                                                                                                                                                                                                                                                                              
    >>> y                                                                                                                                                                                                                                                                                                  
    {'a': 30}                                                                                                                                                                                                                                                                                              
    

    Here o and x refer to the same dict o['a'] and x['a'], and that dict is "mutable" in the sense that you can change the value for key 'a'. That's why "y" needs to be a copy and y['a'] can refer to something else.

提交回复
热议问题