自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

分享幾個簡單易懂的Python技巧,能夠極大的提高工作效率哦!

開發(fā) 后端
今天和大家來分享幾個關于Python的小技巧,都是非常簡單易懂的內(nèi)容,希望大家看了之后能夠有所收獲。

 [[414053]]

今天和大家來分享幾個關于Python的小技巧,都是非常簡單易懂的內(nèi)容,希望大家看了之后能夠有所收獲。

01. 將字符串倒轉(zhuǎn) 

  1. my_string = "ABCDE"  
  2. reversed_string = my_string[::-1]  
  3. print(reversed_string)  
  4. --------------------------------------  
  5. # Output  
  6. # EDCBA 

02. 將英文單詞的首字母大寫

通過title()方法來實現(xiàn)首字母的大寫 

  1. my_string = "my name is xiao ming"  
  2. # 通過title()來實現(xiàn)首字母大寫  
  3. new_string = my_string.title() 
  4.  print(new_string)  
  5. -------------------------------------  
  6. # output  
  7. # My Name Is Xiao Ming 

03. 給字符串去重 

  1. my_string = "aabbbbbccccddddeeeff"  
  2. # 通過set()來進行去重  
  3. temp_set = set(my_string)  
  4. # 通過join()來進行連接  
  5. new_string = ''.join(temp_set)  
  6. print(new_string)  
  7. --------------------------------  
  8. # output  
  9. # dfbcae 

04. 拆分字符串

Python split()通過指定分隔符對字符串進行切片,默認的分隔符是" " 

  1. string_1 = "My name is xiao ming"  
  2. string_2 = "sample, string 1, string 2"  
  3. # 默認的分隔符是空格,來進行拆分  
  4. print(string_1.split())  
  5. # 根據(jù)分隔符","來進行拆分  
  6. print(string_2.split(','))  
  7. ------------------------------------  
  8. # output  
  9. # ['My', 'name', 'is', 'xiao', 'ming']  
  10. # ['sample', ' string 1', ' string 2'] 

05. 將字典中的字符串連詞成串 

  1. list_of_strings = ['My', 'name', 'is', 'Xiao', 'Ming']  
  2. # 通過空格和join來連詞成句  
  3. print(' '.join(list_of_strings))  
  4. -----------------------------------------  
  5. # output  
  6. # My name is Xiao Ming 

06. 查看列表中各元素出現(xiàn)的個數(shù) 

  1. from collections import Counter  
  2. my_list = ['a','a','b','b','b','c','d','d','d','d','d']  
  3. count = Counter(my_list)   
  4. print(count)   
  5. # Counter({'d': 5, 'b': 3, 'a': 2, 'c': 1})  
  6. print(count['b']) # 單獨的“b”元素出現(xiàn)的次數(shù)  
  7. # 3  
  8. print(count.most_common(1)) # 出現(xiàn)頻率最多的元素  
  9. # [('d', 5)] 

07. 合并兩字典 

  1. dict_1 = {'apple': 9, 'banana': 6}  
  2. dict_2 = {'grape': 4, 'orange': 8}  
  3. # 方法一  
  4. combined_dict = {**dict_1, **dict_2}  
  5. print(combined_dict)  
  6. # 方法二  
  7. dict_1.update(dict_2)  
  8. print(dict_1)  
  9. # 方法三  
  10. print(dict(dict_1.items() | dict_2.items()))  
  11. ---------------------------------------  
  12. # output   
  13. # {'apple': 9, 'banana': 6, 'grape': 4, 'orange': 8}  
  14. # {'apple': 9, 'banana': 6, 'grape': 4, 'orange': 8}  
  15. # {'apple': 9, 'banana': 6, 'grape': 4, 'orange': 8} 

08. 查看程序運行的時間 

  1. import time  
  2. start_time = time.time()  
  3. ########################  
  4. # 具體的程序..........  
  5. ########################  
  6. end_time = time.time()  
  7. time_taken_in_micro = (end_time- start_time) * (10 ** 6)  
  8. print(time_taken_in_micro) 

09. 列表的扁平化

有時候會存在列表當中還嵌套著列表的情況, 

  1. from iteration_utilities import deepflatten  
  2. l = [[1,2,3],[4,[5],[6,7]],[8,[9,[10]]]]  
  3. print(list(deepflatten(l, depth=3)))  
  4. -----------------------------------------  
  5. # output  
  6. # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

10. 查看列表當中是否存在重復值 

  1. def unique(l):  
  2.     if len(l)==len(set(l)):  
  3.         print("不存在重復值")  
  4.     else: 
  5.          print("存在重復值")  
  6. unique([1,2,3,4])  
  7. # 不存在重復值  
  8. unique([1,1,2,3])  
  9. # 存在重復值 

11. 數(shù)組的轉(zhuǎn)置 

  1. array = [['a', 'b'], ['c', 'd'], ['e', 'f']]  
  2. transposed = zip(*array)  
  3. print(list(transposed))   
  4. ------------------------------------------  
  5. # output  
  6. # [('a', 'c', 'e'), ('b', 'd', 'f')] 

12. 找出兩列表當中的不同元素 

  1. def difference(a, b):  
  2.     setset_a = set(a)  
  3.     setset_b = set(b)  
  4.     comparison = set_a.difference(set_b)  
  5.     return list(comparison)  
  6. # 返回第一個列表的不同的元素  
  7. difference([1,2,6], [1,2,5])  
  8. # [6] 

13. 將兩列表變成鍵值對

將兩個列表合并成一個鍵值對的字典 

  1. def to_dictionary(keys, values):  
  2.     return dict(zip(keys, values))      
  3. keys = ["a", "b", "c"]      
  4. values = [2, 3, 4]  
  5. print(to_dictionary(keys, values))  
  6. -------------------------------------------  
  7. # output  
  8. # {'a': 2, 'b': 3, 'c': 4} 

14. 對字典進行排序

根據(jù)字典當中的值對字典進行排序 

  1. d = {'apple': 9, 'grape': 4, 'banana': 6, 'orange': 8}  
  2. # 方法一  
  3. sorted(d.items(), key = lambda x: x[1]) # 從小到大排序  
  4. # [('grape', 4), ('banana', 6), ('orange', 8), ('apple', 9)]  
  5. sorted(d.items(), key = lambda x: x[1], reverse = True) # 從大到小排序  
  6. # [('apple', 9), ('orange', 8), ('banana', 6), ('grape', 4)]  
  7. # 方法二  
  8. from operator import itemgetter  
  9. print(sorted(d.items(), key = itemgetter(1)))  
  10. # [('grape', 4), ('banana', 6), ('orange', 8), ('apple', 9)] 

15. 列表中最大/最小值的索引 

  1. list1 = [20, 30, 50, 70, 90]  
  2. def max_index(list_test):  
  3.     return max(range(len(list_test)), key = list_test.__getitem__)  
  4. def min_index(list_test):  
  5.     return min(range(len(list_test)), key = list_test.__getitem__)  
  6. max_index(list1)  
  7. # 4  
  8. min_index(list1)  
  9. # 0  

 

責任編輯:龐桂玉 來源: 深度學習這件小事
相關推薦

2009-05-15 16:36:34

EclipseIDE效率

2019-08-30 14:25:03

Vim命令Linux

2023-07-14 10:54:00

Linux命令

2009-05-14 11:43:56

2022-06-28 07:32:13

JSON字符串POJO

2018-06-11 10:38:56

Vim使用技巧

2011-03-22 14:57:58

2023-10-24 17:45:31

AI

2019-04-03 09:58:00

GitHub代碼開發(fā)者

2011-09-13 19:46:57

2012-03-12 13:35:10

開發(fā)

2025-02-21 09:54:12

2009-07-06 13:38:29

JSPInitJSPDestory

2012-07-04 15:42:22

Web

2020-03-20 11:49:20

Linux命令技巧

2019-07-17 05:02:14

物聯(lián)網(wǎng)工作效率IOT

2020-11-26 10:29:01

Redis

2020-03-25 08:26:44

console.log前端

2025-02-18 10:56:18

2020-12-11 10:00:17

工具代碼Windows
點贊
收藏

51CTO技術棧公眾號