分享幾個簡單易懂的Python技巧,能夠極大的提高工作效率哦!
作者:佚名
今天和大家來分享幾個關于Python的小技巧,都是非常簡單易懂的內(nèi)容,希望大家看了之后能夠有所收獲。
今天和大家來分享幾個關于Python的小技巧,都是非常簡單易懂的內(nèi)容,希望大家看了之后能夠有所收獲。
01. 將字符串倒轉(zhuǎn)
- my_string = "ABCDE"
- reversed_string = my_string[::-1]
- print(reversed_string)
- --------------------------------------
- # Output
- # EDCBA
02. 將英文單詞的首字母大寫
通過title()方法來實現(xiàn)首字母的大寫
- my_string = "my name is xiao ming"
- # 通過title()來實現(xiàn)首字母大寫
- new_string = my_string.title()
- print(new_string)
- -------------------------------------
- # output
- # My Name Is Xiao Ming
03. 給字符串去重
- my_string = "aabbbbbccccddddeeeff"
- # 通過set()來進行去重
- temp_set = set(my_string)
- # 通過join()來進行連接
- new_string = ''.join(temp_set)
- print(new_string)
- --------------------------------
- # output
- # dfbcae
04. 拆分字符串
Python split()通過指定分隔符對字符串進行切片,默認的分隔符是" "
- string_1 = "My name is xiao ming"
- string_2 = "sample, string 1, string 2"
- # 默認的分隔符是空格,來進行拆分
- print(string_1.split())
- # 根據(jù)分隔符","來進行拆分
- print(string_2.split(','))
- ------------------------------------
- # output
- # ['My', 'name', 'is', 'xiao', 'ming']
- # ['sample', ' string 1', ' string 2']
05. 將字典中的字符串連詞成串
- list_of_strings = ['My', 'name', 'is', 'Xiao', 'Ming']
- # 通過空格和join來連詞成句
- print(' '.join(list_of_strings))
- -----------------------------------------
- # output
- # My name is Xiao Ming
06. 查看列表中各元素出現(xiàn)的個數(shù)
- from collections import Counter
- my_list = ['a','a','b','b','b','c','d','d','d','d','d']
- count = Counter(my_list)
- print(count)
- # Counter({'d': 5, 'b': 3, 'a': 2, 'c': 1})
- print(count['b']) # 單獨的“b”元素出現(xiàn)的次數(shù)
- # 3
- print(count.most_common(1)) # 出現(xiàn)頻率最多的元素
- # [('d', 5)]
07. 合并兩字典
- dict_1 = {'apple': 9, 'banana': 6}
- dict_2 = {'grape': 4, 'orange': 8}
- # 方法一
- combined_dict = {**dict_1, **dict_2}
- print(combined_dict)
- # 方法二
- dict_1.update(dict_2)
- print(dict_1)
- # 方法三
- print(dict(dict_1.items() | dict_2.items()))
- ---------------------------------------
- # output
- # {'apple': 9, 'banana': 6, 'grape': 4, 'orange': 8}
- # {'apple': 9, 'banana': 6, 'grape': 4, 'orange': 8}
- # {'apple': 9, 'banana': 6, 'grape': 4, 'orange': 8}
08. 查看程序運行的時間
- import time
- start_time = time.time()
- ########################
- # 具體的程序..........
- ########################
- end_time = time.time()
- time_taken_in_micro = (end_time- start_time) * (10 ** 6)
- print(time_taken_in_micro)
09. 列表的扁平化
有時候會存在列表當中還嵌套著列表的情況,
- from iteration_utilities import deepflatten
- l = [[1,2,3],[4,[5],[6,7]],[8,[9,[10]]]]
- print(list(deepflatten(l, depth=3)))
- -----------------------------------------
- # output
- # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
10. 查看列表當中是否存在重復值
- def unique(l):
- if len(l)==len(set(l)):
- print("不存在重復值")
- else:
- print("存在重復值")
- unique([1,2,3,4])
- # 不存在重復值
- unique([1,1,2,3])
- # 存在重復值
11. 數(shù)組的轉(zhuǎn)置
- array = [['a', 'b'], ['c', 'd'], ['e', 'f']]
- transposed = zip(*array)
- print(list(transposed))
- ------------------------------------------
- # output
- # [('a', 'c', 'e'), ('b', 'd', 'f')]
12. 找出兩列表當中的不同元素
- def difference(a, b):
- setset_a = set(a)
- setset_b = set(b)
- comparison = set_a.difference(set_b)
- return list(comparison)
- # 返回第一個列表的不同的元素
- difference([1,2,6], [1,2,5])
- # [6]
13. 將兩列表變成鍵值對
將兩個列表合并成一個鍵值對的字典
- def to_dictionary(keys, values):
- return dict(zip(keys, values))
- keys = ["a", "b", "c"]
- values = [2, 3, 4]
- print(to_dictionary(keys, values))
- -------------------------------------------
- # output
- # {'a': 2, 'b': 3, 'c': 4}
14. 對字典進行排序
根據(jù)字典當中的值對字典進行排序
- d = {'apple': 9, 'grape': 4, 'banana': 6, 'orange': 8}
- # 方法一
- sorted(d.items(), key = lambda x: x[1]) # 從小到大排序
- # [('grape', 4), ('banana', 6), ('orange', 8), ('apple', 9)]
- sorted(d.items(), key = lambda x: x[1], reverse = True) # 從大到小排序
- # [('apple', 9), ('orange', 8), ('banana', 6), ('grape', 4)]
- # 方法二
- from operator import itemgetter
- print(sorted(d.items(), key = itemgetter(1)))
- # [('grape', 4), ('banana', 6), ('orange', 8), ('apple', 9)]
15. 列表中最大/最小值的索引
- list1 = [20, 30, 50, 70, 90]
- def max_index(list_test):
- return max(range(len(list_test)), key = list_test.__getitem__)
- def min_index(list_test):
- return min(range(len(list_test)), key = list_test.__getitem__)
- max_index(list1)
- # 4
- min_index(list1)
- # 0
責任編輯:龐桂玉
來源:
深度學習這件小事