日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
分享幾個簡單易懂的Python技巧,能夠極大的提高工作效率哦!

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

創(chuàng)新互聯(lián):成立于2013年為各行業(yè)開拓出企業(yè)自己的“網(wǎng)站建設(shè)”服務(wù),為上1000+公司企業(yè)提供了專業(yè)的網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計和網(wǎng)站推廣服務(wù), 按需搭建網(wǎng)站由設(shè)計師親自精心設(shè)計,設(shè)計的效果完全按照客戶的要求,并適當(dāng)?shù)奶岢龊侠淼慕ㄗh,擁有的視覺效果,策劃師分析客戶的同行競爭對手,根據(jù)客戶的實際情況給出合理的網(wǎng)站構(gòu)架,制作客戶同行業(yè)具有領(lǐng)先地位的。

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()來進(jìn)行去重  
  3. temp_set = set(my_string)  
  4. # 通過join()來進(jìn)行連接  
  5. new_string = ''.join(temp_set)  
  6. print(new_string)  
  7. --------------------------------  
  8. # output  
  9. # dfbcae 

04. 拆分字符串

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

 
 
 
  1. string_1 = "My name is xiao ming"  
  2. string_2 = "sample, string 1, string 2"  
  3. # 默認(rèn)的分隔符是空格,來進(jìn)行拆分  
  4. print(string_1.split())  
  5. # 根據(jù)分隔符","來進(jìn)行拆分  
  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. 列表的扁平化

有時候會存在列表當(dāng)中還嵌套著列表的情況,

 
 
 
  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. 查看列表當(dāng)中是否存在重復(fù)值 

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

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. 找出兩列表當(dāng)中的不同元素 

 
 
 
  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ìn)行排序

根據(jù)字典當(dāng)中的值對字典進(jìn)行排序

 
 
 
  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  

分享標(biāo)題:分享幾個簡單易懂的Python技巧,能夠極大的提高工作效率哦!
地址分享:http://www.dlmjj.cn/article/copejie.html