Python_字符串连接
#join() 与split()相反,join()方法用来将列表中多个字符串进行连接,并在相邻两个字符串之间插入指定字符
li=['apple','peach','banana','pear']
sep=','
s=sep.join(li)
print(s) #使用逗号作为连接符
s1=':'.join(li) #使用冒号作为连接符
print(s1)
s2=''.join(li)
print(s2)
#使用split()和join()方法可以删除字符串中多余的空白字符,如果有连续多个空白字符,只保留一个
x='aaa bb c d e fff'
print(' '.join(x.split())) def equalilent(s1,s2): #判断两个字符串在python意义上是否等价
if s1 == s2:
return True
elif ' '.join(s1.split()) == ' '.join(s2.split()):
return True
elif ''.join(s1.split()) == ''.join(s2.split()):
return True
else:
return False
print(equalilent('pip list','pip list'))
# True
print(equalilent('[1,2,3]','[1,2,3]')) #判断两个列表写法是否等价
# True
print(equalilent('[1,2,3]','[1,2, 3]'))
# True
print(equalilent('[1,2,3','[1,2 ,3,4]'))
# False
'''使用运算符"+"也可以连接字符串,但该运算符设计大量数据的复制,效率非常低,不适合大量长字符串的连接。''' import timeit #使用列表推导式生成10000个字符串
strlist = ['This is a long string that will not keep in memory.' for n in range(10000)] #使用字符串对象的join()方法连接多个字符串
def use_join():
return ''.join(strlist) #使用运算符"+"连接多个字符串
def use_plus():
result=''
for strtemp in strlist:
result = result+strtemp
return result if __name__=='__main__':
#重复运行次数
times=1000
jointimer = timeit.Timer('use_join()','from __main__ import use_join')
print('time for join:',jointimer.timeit(number=times))
# time for join: 0.1647309590189252
plustimer = timeit.Timer('use_plus()','from __main__ import use_plus')
print('time for plus:',plustimer.timeit(number=times))
# time for plus: 2.045372327003861
Python_字符串连接的更多相关文章
- 关于python字符串连接的操作
python字符串连接的N种方式 注:本文转自http://www.cnblogs.com/dream397/p/3925436.html 这是一篇不错的文章 故转 python中有很多字符串连接方式 ...
- js ES6 多行字符串 连接字符串
1. 以前,js多行字符串用\n写起来比较费事,所以最新的ES6标准新增了一种多行字符串的表示方法,用` ... `表示: 旧版写法 alert("你好,\n 我叫\n Olive" ...
- python字符串连接的N种方式
python中有很多字符串连接方式,今天在写代码,顺便总结一下: 最原始的字符串连接方式:str1 + str2 python 新字符串连接语法:str1, str2 奇怪的字符串方式:str1 st ...
- SQL注入的字符串连接函数
在select数据时,我们往往需要将数据进行连接后进行回显.很多的时候想将多个数据或者多行数据进行输出的时候,需要使用字符串连接函数.在sqli中,常见的字符串连接函数有concat(),group_ ...
- HDU 1000 & HDU1001 & 字符串连接
A + B Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Swift语言—有趣的字符串连接、数组、字典
字符串链接:Swift语言中的字符串连接方式本人觉得非常的有趣,变量连接需要用右斜杠,并且变量名要括起来 “\(变量名)”,后面的字符串连接分别用逗号 ‘ , ’ 隔开 数组: Var arr = [ ...
- python 字符串连接
字符串连接 方法1: 用字符串的join方法 a = ['a','b','c','d']content = ''content = ''.join(a)print content 方法2: 用字符串的 ...
- C 语言字符串连接的 3种方式
C 语言字符串连接的 3种方式 #include<stdio.h> #include<stdlib.h> #include<string.h> char *join ...
- php大力力 [024节]PHP中的字符串连接操作(2015-08-27)
2015-08-27 php大力力024.PHP中的字符串连接操作 PHP中的字符串连接操作 阅读:次 时间:2012-03-25 PHP字符串的连接的简单实例 时间:2013-12-30 很多 ...
随机推荐
- NSAttributedString富文本简单介绍和常用方法浅析
NSAttributedString基本知识点介绍 1.初始化方法 - (instancetype)initWithString:(NSString *)str; - (instancetype)in ...
- 解决UIScrollView,UIImageView等控件不能响应touch事件的问题
关于UIScrollView,UIImageView等控件不能响应touch事件,主要涉及到事件响应者链的问题,如果在UIScrollView,UIImageView等控件添加了子View,这样事件响 ...
- Leetcode_171_Excel Sheet Column Number
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42290079 Given a column title a ...
- android 开源图表库MPChart最简单使用方法示例教程Demo--折线图 柱状图
转载请注明本文出处:http://blog.csdn.net/wingichoy/article/details/50428246 MPChart是android上一款强大的图表开源库,他可以轻松的绘 ...
- 深度剖析linux内核万能--双向链表,Hash链表模版
我们都知道,链表是数据结构中用得最广泛的一种数据结构,对于数据结构,有顺序存储,数组就是一种.有链式存储,链表算一种.当然还有索引式的,散列式的,各种风格的说法,叫法层出不穷,但是万变不离其中,只要知 ...
- linux内核中的C语言常规算法(前提:你的编译器要支持typeof和type)
学过C语言的伙伴都知道,曾经比较两个数,输出最大或最小的一个,或者是比较三个数,输出最大或者最小的那个,又或是两个数交换,又或是绝对值等等,其实这些算法在linux内核中通通都有实现,以下的代码是我从 ...
- Linux 套接字编程中的 5 个隐患(转)
本文转自IBM博文Linux 套接字编程中的 5 个隐患. “在异构环境中开发可靠的网络应用程序”. Socket API 是网络应用程序开发中实际应用的标准 API.尽管该 API 简单,但是开发新 ...
- RecyclerView 实现gallery画廊效果
1.RecyclerView的基本用法 首先主Activity的布局文件: [html] view plaincopy <RelativeLayout xmlns:android="h ...
- wince6.0 编译报错:"error C2220: warning treated as error - no 'object' file generated"的解决办法
内容提要:wince6.0编译报错:"error C2220: warning treated as error - no 'object' file generated" 原因是 ...
- 导入android SlidingMenu 应用
SlidingMenu is a helpful Android library for developers. It creates a side navigation like the Faceb ...