如何复制 Python 字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24804453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:13:39  来源:igfitidea点击:

How can I copy a Python string?

pythonstringpython-2.7

提问by usual me

I do this:

我这样做:

a = 'hello'

And now I just want an independent copy of a:

现在我只想要一个独立的副本a

import copy

b = str(a)
c = a[:]
d = a + ''
e = copy.copy(a)

map( id, [ a,b,c,d,e ] )

Out[3]:

出[3]:

[4365576160, 4365576160, 4365576160, 4365576160, 4365576160]

Why do they all have the same memory address and how can I get a copy of a?

为什么它们都具有相同的内存地址,我怎样才能获得a?

采纳答案by Martijn Pieters

You don't needto copy a Python string. They are immutable, and the copymodule always returns the original in such cases, as do str(), the whole string slice, and concatenating with an empty string.

不需要复制 Python 字符串。它们是不可变的,copy在这种情况下,模块总是返回原始值,就像 do 一样str(),整个字符串切片,并与空字符串连接。

Moreover, your 'hello'string is interned(certain strings are). Python deliberately tries to keep just the one copy, as that makes dictionary lookups faster.

此外,您的'hello'字符串是实习的某些字符串是)。Python 特意尝试只保留一个副本,因为这会使字典查找速度更快。

One way you could work around this is to actually create a new string, then slice that string back to the original content:

您可以解决此问题的一种方法是实际创建一个新字符串,然后将该字符串切回原始内容:

>>> a = 'hello'
>>> b = (a + '.')[:-1]
>>> id(a), id(b)
(4435312528, 4435312432)

But all you are doing now is waste memory. It is not as if you can mutate these string objects in any way, after all.

但是你现在所做的只是浪费内存。毕竟,您似乎无法以任何方式改变这些字符串对象。

If all you wanted to know is how much memory a Python object requires, use sys.getsizeof(); it gives you the memory footprint of any Python object.

如果您只想知道 Python 对象需要多少内存,请使用sys.getsizeof(); 它为您提供任何 Python 对象的内存占用。

For containers this does notinclude the contents; you'd have to recurse into each container to calculate a total memory size:

对于集装箱这并没有包括的内容; 你必须递归到每个容器来计算总内存大小:

>>> import sys
>>> a = 'hello'
>>> sys.getsizeof(a)
42
>>> b = {'foo': 'bar'}
>>> sys.getsizeof(b)
280
>>> sys.getsizeof(b) + sum(sys.getsizeof(k) + sys.getsizeof(v) for k, v in b.items())
360

You can then choose to use id()tracking to take an actual memory footprint or to estimate a maximum footprint if objects were not cached and reused.

然后,您可以选择使用id()跟踪来获取实际内存占用量,或者在对象未被缓存和重用的情况下估计最大占用量。

回答by Richard Urban

You can copy a string in python via string formatting :

您可以通过字符串格式在 python 中复制字符串:

>>> a = 'foo'  
>>> b = '%s' % a  
>>> id(a), id(b)  
(140595444686784, 140595444726400)  

回答by Thomas Youngson

Copying a string can be done two ways either copy the location a = "a" b = a or you can clone which means b wont get affected when a is changed which is done by a = 'a' b = a[:]

复制字符串可以通过两种方式完成:复制位置 a = "a" b = a 或者您可以克隆,这意味着当 a 更改时 b 不会受到影响,这是由 a = 'a' b = a[:]

回答by karl s

I'm just starting some string manipulations and found this question. I was probably trying to do something like the OP, "usual me". The previous answers did not clear up my confusion, but after thinking a little about it I finally "got it".

我刚刚开始一些字符串操作并发现了这个问题。我可能正在尝试做一些类似于 OP 的事情,“通常的我”。前面的回答并没有解决我的困惑,但经过一番思考后,我终于“明白了”。

As long as a, b, c, d, and ehave the same value, they reference to the same place. Memory is saved. As soon as the variable start to have different values, they get start to have different references. My learning experience came from this code:

只要abcd,并且e具有相同的价值,他们引用了同一个地方。内存被保存。一旦变量开始具有不同的值,它们就会开始具有不同的引用。我的学习经验来自这段代码:

import copy
a = 'hello'
b = str(a)
c = a[:]
d = a + ''
e = copy.copy(a)

print map( id, [ a,b,c,d,e ] )

print a, b, c, d, e

e = a + 'something'
a = 'goodbye'
print map( id, [ a,b,c,d,e ] )
print a, b, c, d, e

The printed output is:

打印输出为:

[4538504992, 4538504992, 4538504992, 4538504992, 4538504992]

hello hello hello hello hello

[6113502048, 4538504992, 4538504992, 4538504992, 5570935808]

goodbye hello hello hello hello something

回答by Charles Thayer

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.

换句话说,“id()”不是你关心的。您想知道是否可以在不损害源变量名称的情况下修改变量名称。

>>> 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.

如果您习惯使用 C,那么这些就像指针变量一样,只是您不能取消引用它们来修改它们指向的内容,但是 id() 会告诉您它们当前指向的位置。

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

当您考虑更深层次的结构(如列表或字典)时,python 程序员的问题就出现了:

>>> 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.

这里 o 和 x 指代相同的 dict o['a'] 和 x['a'],并且该 dict 是“可变的”,因为您可以更改键 'a' 的值。这就是为什么“y”需要是一个副本而 y['a'] 可以指代其他东西。