Python .join or string concatenation

后端 未结 6 687
小蘑菇
小蘑菇 2021-01-31 18:23

I realise that if you have an iterable you should always use .join(iterable) instead of for x in y: str += x. But if there\'s only a fixed number of va

相关标签:
6条回答
  • 2021-01-31 18:38

    I use next:

    ret = '%s@%s' % (user, host)
    
    0 讨论(0)
  • 2021-01-31 18:48

    (I'm pretty sure all of the people pointing at string formatting are missing the question entirely.)

    Creating a string by constructing an array and joining it is for performance reasons only. Unless you need that performance, or unless it happens to be the natural way to implement it anyway, there's no benefit to doing that rather than simple string concatenation.

    Saying '@'.join([user, host]) is unintuitive. It makes me wonder: why is he doing this? Are there any subtleties to it; is there any case where there might be more than one '@'? The answer is no, of course, but it takes more time to come to that conclusion than if it was written in a natural way.

    Don't contort your code merely to avoid string concatenation; there's nothing inherently wrong with it. Joining arrays is just an optimization.

    0 讨论(0)
  • 2021-01-31 18:48

    I recommend join() over concatenation, based on two aspects:

    1. Faster.
    2. More elegant.

    Regarding the first aspect, here's an example:

    import timeit    
    
    s1 = "Flowers"    
    s2 = "of"    
    s3 = "War"    
    
    def join_concat():    
        return s1 + " " + s2 + " " + s3  
    
    def join_builtin():    
        return " ".join((s1, s2, s3))    
    
    print("Join Concatenation: ", timeit.timeit(join_concat))         
    print("Join Builtin:       ", timeit.timeit(join_builtin))
    

    The output:

    $ python3 join_test.py
    Join Concatenation:  0.40386943198973313
    Join Builtin:        0.2666833929979475
    

    Considering a huge dataset (millions of lines) and its processing, 130 milliseconds per line, it's too much.

    And for the second aspect, indeed, is more elegant.

    0 讨论(0)
  • 2021-01-31 18:54

    If you're creating a string like that, you normally want to use string formatting:

    >>> user = 'username'
    >>> host = 'host'
    >>> '%s@%s' % (user, host)
    'username@host'
    

    Python 2.6 added another form, which doesn't rely on operator overloading and has some extra features:

    >>> '{0}@{1}'.format(user, host)
    'username@host'
    

    As a general guideline, most people will use + on strings only if they're adding two strings right there. For more parts or more complex strings, they either use string formatting, like above, or assemble elements in a list and join them together (especially if there's any form of looping involved.) The reason for using str.join() is that adding strings together means creating a new string (and potentially destroying the old ones) for each addition. Python can sometimes optimize this away, but str.join() quickly becomes clearer, more obvious and significantly faster.

    0 讨论(0)
  • 2021-01-31 19:02

    I'll just note that I've always tended to use in-place concatenation until I was rereading a portion of the Python general style PEP PEP-8 Style Guide for Python Code.

    • Code should be written in a way that does not disadvantage other implementations of Python (PyPy, Jython, IronPython, Pyrex, Psyco, and such). For example, do not rely on CPython's efficient implementation of in-place string concatenation for statements in the form a+=b or a=a+b. Those statements run more slowly in Jython. In performance sensitive parts of the library, the ''.join() form should be used instead. This will ensure that concatenation occurs in linear time across various implementations.

    Going by this, I have been converting to the practice of using joins so that I may retain the habit as a more automatic practice when efficiency is extra critical.

    So I'll put in my vote for:

    ret = '@'.join([user, host])
    
    0 讨论(0)
  • 2021-01-31 19:04

    I take the question to mean: "Is it ok to do this:"

    ret = user + '@' + host
    

    ..and the answer is yes. That is perfectly fine.

    You should, of course, be aware of the cool formatting stuff you can do in Python, and you should be aware that for long lists, "join" is the way to go, but for a simple situation like this, what you have is exactly right. It's simple and clear, and performance will not be an issue.

    0 讨论(0)
提交回复
热议问题