用途
str.join():
s = ''.join(l)
您在其上调用的字符串用作以下字符串之间的定界符
l:
>>> l=['a', 'b', 'c']>>> ''.join(l)'abc'>>> '-'.join(l)'a-b-c'>>> ' - spam ham and eggs - '.join(l)'a - spam ham and eggs - b - spam ham and eggs - c'
使用
str.join()是 多少 不是连接你的元素一一更快,因为这有可能创造为每个级联一个新的字符串对象。
str.join()只需创建 一个
新的字符串对象。
注意,
str.join()它将在输入序列上循环 两次
。一次计算输出字符串需要多大,再一次构建它。作为副作用,这意味着使用列表理解而不是生成器表达式会更快:
slower_gen_expr = ' - '.join('{}: {}'.format(key, value) for key, value in some_dict)faster_list_comp = ' - '.join(['{}: {}'.format(key, value) for key, value in some_dict])


