(附代码)分享 | 几个简单易懂的Python技巧,极大的提高工作效率
点击左上方蓝字关注我们
转载自 | 机器学习初学者
my_string = "ABCDE"
reversed_string = my_string[::-1]
print(reversed_string)
--------------------------------------
# Output
# EDCBA
my_string = "my name is xiao ming"
# 通过title()来实现首字母大写
new_string = my_string.title()
print(new_string)
-------------------------------------
# output
# My Name Is Xiao Ming
my_string = "aabbbbbccccddddeeeff"
# 通过set()来进行去重
temp_set = set(my_string)
# 通过join()来进行连接
new_string = ''.join(temp_set)
print(new_string)
--------------------------------
# output
# dfbcae
Python split()通过指定分隔符对字符串进行切片,默认的分隔符是" "
string_1 = "My name is xiao ming"
string_2 = "sample, string 1, string 2"
# 默认的分隔符是空格,来进行拆分
print(string_1.split())
# 根据分隔符","来进行拆分
print(string_2.split(','))
------------------------------------
# output
# ['My', 'name', 'is', 'xiao', 'ming']
# ['sample', ' string 1', ' string 2']
list_of_strings = ['My', 'name', 'is', 'Xiao', 'Ming']
# 通过空格和join来连词成句
print(' '.join(list_of_strings))
-----------------------------------------
# output
# My name is Xiao Ming
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”元素出现的次数
# 3
print(count.most_common(1)) # 出现频率最多的元素
# [('d', 5)]
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}
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)
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]
def unique(l):
if len(l)==len(set(l)):
print("不存在重复值")
else:
print("存在重复值")
unique([1,2,3,4])
# 不存在重复值
unique([1,1,2,3])
# 存在重复值
array = [['a', 'b'], ['c', 'd'], ['e', 'f']]
transposed = zip(*array)
print(list(transposed))
------------------------------------------
# output
# [('a', 'c', 'e'), ('b', 'd', 'f')]
def difference(a, b):
set_a = set(a)
set_b = set(b)
comparison = set_a.difference(set_b)
return list(comparison)
# 返回第一个列表的不同的元素
[1,2,5])
# [6]
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}
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)]
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
END
双一流大学研究生团队创建,专注于目标检测与深度学习,希望可以将分享变成一种习惯! 整理不易,点赞三连↓
评论