如何将多个小字符串拼接成一个大字符串?

需求:
在设计网络程序时,我们自定义了一个基于UDP的网络协议,按照固定次序向服务器传递一系列参数:
hwDetect: "<0112>"
gxDepthBits: "<32>"
gxResolution: "<1024x768>"
gxRefresh: "<60>"
fullAlpha: "<1>"
lodDist: "<100.0>"
DistCull: "<500.0>"

在程序中我们将各个参数按次序收集到列表中:
["<0112>","<32>","<1024x768>","<60>","<1>","<100.0>","<500.0>"]
最终我们要把各个参数拼接成一个数据报进行发送.
"<0112><32><1024x768><60><1><100.0><500.0>"

思路:
1、迭代列表,连续使用'+'操作依次拼接每一个字符串
2、使用str.join()方法,更加快速的拼接列表中所有字符串

代码:

s = ["<0112>","<32>","<1024x768>","<60>","<1>","<100.0>","<500.0>"]

#方法1:
a = ''
for x in s:
    a += x

print(a)

#方法2:str.join(self, iterable, /),xample: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
result = ''.join(s)  
print(result)

#附:若列表中有字符串又有整数,该如何快速拼接
l = ['abc',1,34,'cde']

#方法一:利用列表推导式,若列表非常长开销大
ret = ''.join([str(x) for x in l])
print(ret)

#方法二:利用生成器推导式,开销小
ret2 = ''.join(str(x) for x in l)  # 生成器推导式作为参数时,()可以省略。等价于:''.join((str(x) for x in l))。<generator object <genexpr> at 0x0000024375AF49A8>
print(ret2)

======================================================================================
>>> s1 = 'abcdef'

>>> s2 ='12345'

>>> s1 + s2
'abcdef12345'

>>> str.__add__(s2)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-2da1341bdde4> in <module>
----> 1 str.__add__(s2)

TypeError: expected 1 arguments, got 0

>>> s1.__add__(s2)
'abcdef12345'

>>> s2.__add__(s1)
'12345abcdef'

>>> str.__add__(s1,s2)
'abcdef12345'

>>> ["<0112>","<32>","<1024x768>","<60>","<1>","<100.0>","<500.0>"]
['<0112>', '<32>', '<1024x768>', '<60>', '<1>', '<100.0>', '<500.0>']

>>> l = ["<0112>","<32>","<1024x768>","<60>","<1>","<100.0>","<500.0>"]

>>> s = ''

>>> for x in l:
...     s += x
... 

>>> s
'<0112><32><1024x768><60><1><100.0><500.0>'

>>> from functools import reduce

>>> reduce(a,b:a + b ,l)
  File "<ipython-input-19-830264eeda55>", line 1
    reduce(a,b:a + b ,l)
              ^
SyntaxError: invalid syntax


>>> reduce(a,b:a+b,l)
  File "<ipython-input-20-70a6e5629518>", line 1
    reduce(a,b:a+b,l)
              ^
SyntaxError: invalid syntax


>>> reduce(lambda a,b:a+b,l)
'<0112><32><1024x768><60><1><100.0><500.0>'

>>> reduce(str.__add__,l)
'<0112><32><1024x768><60><1><100.0><500.0>'

>>> s = ''

>>> for x in l:
...     s += x
... 

>>> for x in l:
...     s += x
...     print(s)
... 
<0112><32><1024x768><60><1><100.0><500.0><0112>
<0112><32><1024x768><60><1><100.0><500.0><0112><32>
<0112><32><1024x768><60><1><100.0><500.0><0112><32><1024x768>
<0112><32><1024x768><60><1><100.0><500.0><0112><32><1024x768><60>
<0112><32><1024x768><60><1><100.0><500.0><0112><32><1024x768><60><1>
<0112><32><1024x768><60><1><100.0><500.0><0112><32><1024x768><60><1><100.0>
<0112><32><1024x768><60><1><100.0><500.0><0112><32><1024x768><60><1><100.0><500.0>

>>> str.join?
Docstring:
S.join(iterable) -> str

Return a string which is the concatenation of the strings in the
iterable.  The separator between elements is S.
Type:      method_descriptor

>>> l2 = ['abc','efx','1234']

>>> ';'.join(l2)
'abc;efx;1234'

>>> ';=2o4'.join(l2)
'abc;=2o4efx;=2o41234'

>>> ''.join(l2)
'abcefx1234'

>>> '
  File "<ipython-input-31-82eddc4252ed>", line 1
    '
     ^
SyntaxError: EOL while scanning string literal


>>> ''.join(l)
'<0112><32><1024x768><60><1><100.0><500.0>'

>>> timeit ''.join(l)   # 计算执行的时间 
121 ns ± 1.57 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

>>> timeit reduce(str.__add__,l) # 计算执行的时间 
696 ns ± 12.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

```
posted @ 2020-07-09 23:22  Richardo-M-Lu  阅读(328)  评论(0编辑  收藏  举报