目录
字符串反转(字符串逆序输出)代码实现十多种方法对比(解析思路及其耗费时间)详细攻略
网友思路参考 文章:Reverse a string in Python - Stack Overflow
字符串反转(字符串逆序输出)代码实现十多种方法对比(解析思路及其耗费时间)详细攻略
耗费时间对比
方法 | 耗费时间 |
T1、使用字符串切片的方法 | str01: 0.004107 |
T2、使用reversed()方法 | str02: 0.007081 |
T3、使用list和reverser()方法 | str03: 0.006830 |
T4、使用reduce方法 | str04: 0.202377 |
T5、while循环+空列表+字符串拼接 | str05: 0.027280 |
T6、利用list和字符串拼接 | str06: 0.036437 |
T7、while循环+字符串拼接 | str07: 0.036202 |
T8、利用栈的思维 | str08: 0.057554 |
T9、利用递归思想 | 最长! |
T10、利用一行代码(for循环+字符串拼接) | str10: 0.030771 |
T11、利用for循环+倒叙提字母+字合并符串 | str11: 0.027477 |
T1、使用字符串切片的方法
1. import time 2. 3. # str_temp = '123456789' 4. str_temp = '123456789'*10000 5. 6. 7. # T1、使用字符串切片的方法 8. STime1 = time.clock() 9. str01 = str_temp[::-1] 10. print(str01) 11. 12. ETime1 = time.clock() 13. CostTime1=ETime1-STime1 14. print('str01:','%.6f' % CostTime1)
T2、使用reversed()方法
1. # T2、使用reversed()方法 2. STime2 = time.clock() 3. str02 = ''.join(reversed(str_temp)) 4. print(str02) 5. 6. ETime2 = time.clock() 7. CostTime2=ETime2-STime2 8. print('str02:','%.6f' % CostTime2)
T3、使用list和reverser()方法
1. # T3、使用list和reverser()方法 2. STime3 = time.clock() 3. str_temp2list = list(str_temp) 4. str_temp2list.reverse() 5. str03 = ''.join(str_temp2list) 6. print(str03) 7. 8. ETime3 = time.clock() 9. CostTime3=ETime3-STime3 10. print('str03:','%.6f' % CostTime3)
T4、使用reduce方法
1. # T4、使用reduce方法 2. from functools import reduce 3. STime4 = time.clock() 4. str04 = reduce(lambda x,y : y+x, str_temp) 5. print(str04) 6. 7. ETime4 = time.clock() 8. CostTime4=ETime4-STime4 9. print('str04:','%.6f' % CostTime4)
T5、while循环+空列表+字符串拼接
1. # T5、for循环+空列表+字符串拼接 2. STime5 = time.clock() 3. str05 = [] 4. str_temp_len = len(str_temp) 5. while str_temp_len: 6. str_temp_len -= 1 7. str05.append(str_temp[str_temp_len]) 8. print(''.join(str05)) 9. 10. ETime5 = time.clock() 11. CostTime5=ETime5-STime5 12. print('str05:','%.6f' % CostTime5)
T6、利用list和字符串拼接
1. # T6、利用list和字符串拼接 2. STime6 = time.clock() 3. str06 = '' 4. str_temp_len = len(str_temp) 5. for i in range(str_temp_len - 1, -1, -1): 6. str06 += str_temp[i] 7. print(str06) 8. 9. ETime6 = time.clock() 10. CostTime6=ETime6-STime6 11. print('str06:','%.6f' % CostTime6)
T7、while循环+字符串拼接
1. # T7、while循环+字符串拼接 2. STime7 = time.clock() 3. str07 = '' 4. str_temp_len = len(str_temp) 5. while str_temp_len: 6. str_temp_len -= 1 7. str07 += str_temp[str_temp_len] 8. print(str07) 9. 10. ETime7 = time.clock() 11. CostTime7=ETime7-STime7 12. print('str07:','%.6f' % CostTime7)
T8、利用栈的思维
1. # T8、利用栈的思维 2. STime8 = time.clock() 3. str_temp2list = list(str_temp) #模拟全部入栈 4. str08 = "" 5. while len(str_temp2list)>0: 6. str08 += str_temp2list.pop() #模拟出栈 7. print(str08) 8. 9. ETime8 = time.clock() 10. CostTime8=ETime8-STime8 11. print('str08:','%.6f' % CostTime8)
T9、利用递归思想
1. # T9、利用递归思想 2. def r_string(str_temp): 3. if len(str_temp) == 1: 4. return str_temp 5. return str_temp[-1] + r_string(str_temp[:-1]) 6. STime9 = time.clock() 7. str09 = r_string(str_temp) 8. print(str09) 9. 10. ETime9 = time.clock() 11. CostTime9=ETime9-STime9 12. print('str09:','%.6f' % CostTime9)
T10、利用一行代码(for循环+字符串拼接)
1. # T10、利用一行代码(for循环+字符串拼接) 2. STime10 = time.clock() 3. str10 = ''.join(str_temp[len(str_temp) - i - 1] for i in range(len(str_temp))) 4. print(str10) 5. 6. ETime10 = time.clock() 7. CostTime10=ETime10-STime10 8. print('str10:','%.6f' % CostTime10)
T11、利用for循环+倒叙提字母+字合并符串
1. # T11、利用for循环+倒叙提字母+字合并符串 2. STime11 = time.clock() 3. str11='' 4. for i in range(1,len(str_temp)+1): 5. str11=str11+str_temp[-i] 6. print(str11) 7. 8. ETime11 = time.clock() 9. CostTime11=ETime11-STime11 10. print('str11:','%.6f' % CostTime11)