首页 文章

如何在Python中将一个字符串附加到另一个字符串?

提问于
浏览
521

我想要一种有效的方法在Python中将一个字符串附加到另一个字符串 .

var1 = "foo"
var2 = "bar"
var3 = var1 + var2

有没有什么好的内置方法可供使用?

10 回答

  • 3

    如果你只有一个字符串的引用,并且你将另一个字符串连接到结尾,CPython现在特殊情况,并尝试扩展字符串 .

    最终结果是操作是摊销O(n) .

    例如

    s = ""
    for i in range(n):
        s+=str(i)
    

    曾经是O(n ^ 2),但现在是O(n) .

    从源代码(bytesobject.c):

    void
    PyBytes_ConcatAndDel(register PyObject **pv, register PyObject *w)
    {
        PyBytes_Concat(pv, w);
        Py_XDECREF(w);
    }
    
    
    /* The following function breaks the notion that strings are immutable:
       it changes the size of a string.  We get away with this only if there
       is only one module referencing the object.  You can also think of it
       as creating a new string object and destroying the old one, only
       more efficiently.  In any case, don't use this if the string may
       already be known to some other part of the code...
       Note that if there's not enough memory to resize the string, the original
       string object at *pv is deallocated, *pv is set to NULL, an "out of
       memory" exception is set, and -1 is returned.  Else (on success) 0 is
       returned, and the value in *pv may or may not be the same as on input.
       As always, an extra byte is allocated for a trailing \0 byte (newsize
       does *not* include that), and a trailing \0 byte is stored.
    */
    
    int
    _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
    {
        register PyObject *v;
        register PyBytesObject *sv;
        v = *pv;
        if (!PyBytes_Check(v) || Py_REFCNT(v) != 1 || newsize < 0) {
            *pv = 0;
            Py_DECREF(v);
            PyErr_BadInternalCall();
            return -1;
        }
        /* XXX UNREF/NEWREF interface should be more symmetrical */
        _Py_DEC_REFTOTAL;
        _Py_ForgetReference(v);
        *pv = (PyObject *)
            PyObject_REALLOC((char *)v, PyBytesObject_SIZE + newsize);
        if (*pv == NULL) {
            PyObject_Del(v);
            PyErr_NoMemory();
            return -1;
        }
        _Py_NewReference(*pv);
        sv = (PyBytesObject *) *pv;
        Py_SIZE(sv) = newsize;
        sv->ob_sval[newsize] = '\0';
        sv->ob_shash = -1;          /* invalidate cached hash value */
        return 0;
    }
    

    通过经验验证很容易 .

    $ python -m timeit -s"s=''" "for i in xrange(10):s+='a'"
    1000000 loops, best of 3: 1.85 usec per loop
    $ python -m timeit -s"s=''" "for i in xrange(100):s+='a'"
    10000 loops, best of 3: 16.8 usec per loop
    $ python -m timeit -s"s=''" "for i in xrange(1000):s+='a'"
    10000 loops, best of 3: 158 usec per loop
    $ python -m timeit -s"s=''" "for i in xrange(10000):s+='a'"
    1000 loops, best of 3: 1.71 msec per loop
    $ python -m timeit -s"s=''" "for i in xrange(100000):s+='a'"
    10 loops, best of 3: 14.6 msec per loop
    $ python -m timeit -s"s=''" "for i in xrange(1000000):s+='a'"
    10 loops, best of 3: 173 msec per loop
    

    It's important 但是请注意,据我所知,此优化仅在cPython实现中不是't part of the Python spec. It' . 例如,对于pypy或jython的相同经验测试可能会显示较旧的O(n ** 2)性能 .

    $ pypy -m timeit -s"s=''" "for i in xrange(10):s+='a'"
    10000 loops, best of 3: 90.8 usec per loop
    $ pypy -m timeit -s"s=''" "for i in xrange(100):s+='a'"
    1000 loops, best of 3: 896 usec per loop
    $ pypy -m timeit -s"s=''" "for i in xrange(1000):s+='a'"
    100 loops, best of 3: 9.03 msec per loop
    $ pypy -m timeit -s"s=''" "for i in xrange(10000):s+='a'"
    10 loops, best of 3: 89.5 msec per loop
    

    到目前为止很好,但是,

    $ pypy -m timeit -s"s=''" "for i in xrange(100000):s+='a'"
    10 loops, best of 3: 12.8 sec per loop
    

    哎哟比二次更糟糕 . 因此pypy正在做一些适用于短字符串的东西,但对于较大的字符串表现不佳 .

  • 4

    唐't prematurely optimize. If you have no reason to believe there'是字符串连接引起的速度瓶颈,然后坚持 ++=

    s  = 'foo'
    s += 'bar'
    s += 'baz'
    

    也就是说,如果你是StringBuilder,规范的Python习惯用法是将项添加到列表中,然后使用 str.join 将它们连接到最后:

    l = []
    l.append('foo')
    l.append('bar')
    l.append('baz')
    
    s = ''.join(l)
    
  • 9

    别 .

    也就是说,对于大多数情况,您最好一次性生成整个字符串,而不是附加到现有字符串 .

    例如,不要这样做: obj1.name + ":" + str(obj1.count)

    相反:使用 "%s:%d" % (obj1.name, obj1.count)

    这将更容易阅读和更有效 .

  • 38
    str1 = "Hello"
    str2 = "World"
    newstr = " ".join((str1, str2))
    

    这将str1和str2与空格连接为分隔符 . 你也可以做 "".join(str1, str2, ...) . str.join() 采用可迭代的方式,因此您必须将字符串放在列表或元组中 .

    这与内置方法一样高效 .

  • 10

    如果需要执行许多追加操作来构建大字符串,可以使用StringIO或cStringIO . 界面就像一个文件 . 即:你 write 将文字附加到它 .

    如果您只是附加两个字符串,那么只需使用 + .

  • 33

    这真的取决于你的应用程序 . 如果你循环遍历数百个单词并希望将它们全部附加到列表中, .join() 会更好 . 但如果你're putting together a long sentence, you'最好使用 += .

  • 535

    Python 3.6给了我们f-strings,这是一个令人高兴的:

    var1 = "foo"
    var2 = "bar"
    var3 = f"{var1}{var2}"
    print(var3)                       # prints foobar
    

    你可以在花括号内做大部分事情

    print(f"1 + 1 == {1 + 1}")        # prints 1 + 1 == 2
    
  • 3
    a='foo'
    b='baaz'
    
    a.__add__(b)
    
    out: 'foobaaz'
    
  • 2

    基本上没什么区别 . 唯一一致的趋势是Python似乎每个版本都变慢...... :(


    列表

    %%timeit
    x = []
    for i in range(100000000):  # xrange on Python 2.7
        x.append('a')
    x = ''.join(x)
    

    Python 2.7

    1循环,最佳3:每循环7.34秒

    Python 3.4

    1循环,最佳3:7.99秒每循环

    Python 3.5

    1循环,最佳3:每循环8.48秒

    Python 3.6

    1循环,每循环最佳3:9.93秒


    字符串

    %%timeit
    x = ''
    for i in range(100000000):  # xrange on Python 2.7
        x += 'a'
    

    Python 2.7

    1循环,最佳3:每循环7.41秒

    Python 3.4

    1循环,最佳3:每循环9.08秒

    Python 3.5

    1循环,最佳3:每循环8.82秒

    Python 3.6

    1循环,最佳3:每循环9.24秒

  • 256

    add 函数追加字符串

    str = "Hello"
    str2 = " World"
    st = str.__add__(str2)
    print(st)
    

    产量

    Hello World
    

相关问题