参考链接:https://zhuanlan.zhihu.com/p/83998758?utm_source=qq&utm_medium=social&utm_oi=728200852833075200
1.判断是否存在重复元素
def all_unique(a): return len(a)==len(set(a))
print(all_unique([1,1,2,3]))
输出:False
2.检查两个字符串的组成是否一样,即元素的种类和数目是否一致
anagram(a,b): return Counter(a)==Counter(b) print(anagram("3abcda",acdba3")) 输出:True
3.内存占用
#32位系统 import sys variable = 38 print(sys.getsizeof(1)) print(sys.maxsize) 输出:14 2147483647
4.字节占用
byte_size(string): return len(string.encode(utf-8print(byte_size(hello world)) 输出:11
5.打印N次字符串
print(“a”*3)
输出:aaa
6.大写第一个字母
print("abc".title())
输出:Abc
7.分块
from math ceil chunk(alist,size): return list(map(lambda x:alist[x*size:x*size+size],list(range(0,ceil(len(alist)/size))))) print(chunk([1,3,4,5],2)) 输出:[[1,2],[3,4],[5]]
8.压缩
使用python filter函数,其一般形式为filter(func,iterable)
例子1: is_odd(a): return a%2==1 print(list(filter(is_odd,[1,5,6,7,8]))) 输出:[1,7] 例子2: print(list(filter(bool,[False,None,1)">"",1)">a]))) 输出:[3,1)">"]
9.解包
10.链式对比
11.列表转字符串(用逗号相隔)
print(,.join(['a','b','c'])) 输出:a,b,c
12.元音统计(正则表达式的一种应用)
13.展开列表
a=[1,[2],[[3],5 spread(a): res=[] in a: if isinstance(i,list): res.extend(i) else: res.append(i) return res deep_flatten(b): result=[] result.extend(spread((list(map(lambda x:deep_flatten(x) if type(x)==list x,b))))) result (deep_flatten(a)) 输出:[1,5]
14.列表的差(返回第一个列表的元素,不再第二个列表中的)
diff(a,b): set_a=set(a) set_b=set(b) comparison=set_a.difference(set_b) list(comparison) print(diff([1,3],4])) 输出:[3]
15.通过函数取差(如下方法会先应用一个给定的函数,然后再返回应用函数后结果有差别的列表的元素)
math difference_by(a,fn): b=set(map(math.floor,b)) return [item for item in a if math.floor(item) not b] print(difference_by([1.2,2.1],[2.3,3.4],math.floor)) 输出:[1.2]
add(a,1)">return a+b substract(a,1)">return a-b a,b=4,5 print((substract if a>b add)(a,b)) 输出:9
17.判断列表是否有重复值
print(len([1,3]==len(set([1,3]))) 输出:False
18.合并两个字典
merge_two_dicts(a,b): c=a.copy() c.update(b) c print(merge_two_dicts({1:2},{3:4})) 输出:{1:2,3:4} 再pyhton3.5及以上,直接print({**{1:2},**{3:4}})
19.把两个列表转换成字典
to_dictionary(a,1)"> dict(zip(a,b)) print(to_dictionary(["],1)">])) 输出:{":1,1)">":2}
20.使用枚举(能够同时取到index和value)
a=[for index,val enumerate(a): (index,val) 输出: 0 a 1 b
21.执行时间(计算执行特定代码所用的时间)
time start_time=time.time() in range(10000): (i) end_time=time.time() total_time=end_time-start_time print(round(total_time,1)">)) 输出:0.17
22.Try else(可以多加一个else,如果没有触发错误,这个子句就会被执行)
try: 2*3 except TypeError: An exception) : successful) 输出:successful
most_frequent(a): return max(set(a),key=a.count) print(most_frequent([1,1)">])) 输出:2
24.回文序列(会先将所有字母转换成小写字母,并且移除非英文字母符号)
palindrome(string): from re sub s=sub([\W_]""return s==s[::-1print(palindrome(taco cat)) 输出:True
25.不使用if else计算子
operator action={ +:operator.add,1)">-:operator.sub,1)">*:operator.mul,1)">/:operator.truediv,1)">**:pow,} print(action["](50,25)) 输出:25
26.Shuffle(打乱列表排序的顺序)
from copy deepcopy from random randint shuffle(a): tmp_list=deepcopy(a) n=len(tmp_list) while n: n-=1 i=randint(0,n) tmp_list[n],tmp_list[i]=tmp_list[i],tmp_list[n] tmp_list print(shuffle([1,1)">])) 输出:[2,1](每次结果都不一样)