概述
介绍一些python 编程中的一些小技巧,可能有你知道的,可能也有你不知道的,确实是非常有技巧性了,希望能记下来用的时候能想起。
0 1字符串翻转
# 字符串翻转exam = 'hello world'print(exam[::-1])from functools import reduceprint(reduce(lambda x, y: y+x, exam))
dlrow olleh
dlrow olleh
0 2单词大小写
# 单词大小写exam3 = 'i love china'print(exam3.upper()) # 全大写print(exam3.capitalize()) # 字符串首字符大写print(exam3.title()) # 每个单词首字母大写
I LOVE CHINA
I love china
I Love China
0 3字符串拆分
# 字符串拆分exam4 = "I Love China"exam5 = "I/Love/China"exam6 = " I Love China "print(exam4.split()) # 按空格拆分print(exam5.split('/')) # 按/拆分print(exam6.strip()) # 去除字符串两边空格print(exam6.lstrip()) # 去除字符串左边空格print(exam6.rstrip()) # 去除字符串右边空格
['I', 'Love', 'China']
['I', 'Love', 'China']
'I Love China'
'I Love China '
' I Love China'
0 4字符串转化数字# 字符串转数字str = '2123234'#方法一res_1 = list(map(int, str))print(res_1)#方法二res_2 = [int(i) for i in str]print(res_2)
[2, 1, 2, 3, 2, 3, 4]
[2, 1, 2, 3, 2, 3, 4]
0 5去重
# 去重exam7 = 'aabbccddeefffg'print(''.join(set(exam7)))exam8 = [1, 2, 2, 3, 3, 4, 4, 5, 6, 7]print(list(set(exam8)))
fcgebda
[1, 2, 3, 4, 5, 6, 7]
0 6去重扩展(去重且保持顺序不变)
# 去重扩展 (去重且保持顺序不变)def updateList(items):seen = set()for item in items:if item not in seen:yield item
seen.add(item)print(list(updateList(exam8)))
[1, 2, 3, 4, 5, 6, 7]
0 7将列表展开# 将列表展开 方法一res_list = [[1, 2, 3], ['a', 'b', 'c']]print([j for i in res_list for j in i])
# 将列表展开 方法二from iteration_utilities import deepflatten
res_list2 = [[1, 2, 3], [4, 5, [6, 7]], [8], [9, [10, [11]]]]print(list(deepflatten(res_list2)))
# 将列表展开 方法三 递归def flatten(lst):res = []for i in lst:if isinstance(i, list):res.extend(flatten(i))else:res.append(i)return resprint(flatten(res_list2))
[1, 2, 3, 'a', 'b', 'c']
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
0 8统计列表元素个数# 统计列表元素个数from collections import Counter
res_list = [1, 2, 3, 2, 4, 6, 6, 6, 5, 7, 7, 8, 8, 8, 8, 8, 9, 10]count = Counter(res_list)print(count)print(count[6]) # 元素6的个数print(count.most_common(5)) # 最多的前五个元素
Counter({8: 5, 6: 3, 2: 2, 7: 2, 1: 1, 3: 1, 4: 1, 5: 1, 9: 1, 10: 1})
3
[(8, 5), (6, 3), (2, 2), (7, 2), (1, 1)]
0 9判断字符串元素是否相同from collections import Counter
str_1, str_2, str_3 = 'python', 'onthpy', 'nohtyp're_1, re_2, re_3 = Counter(str_1), Counter(str_2), Counter(str_3)result = '元素相同' if (re_1 == re_2 == re_3) else '元素不同'print(result)
'元素相同'
10字典合并# 字典合并dict1 = {'a': 1, 'b': 2}dict2 = {'c': 3, 'd': 4}# 方法一res_dict = {**dict1, **dict2}print(res_dict)# 方法二dict1.update(dict2)print(dict1)
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
11寻找最大/小的 N 个元素# 寻找最大/小的 N 个元素import heapq
list1 = [1, 8, 2, 11, 22, -4, 18, 23, 43, 26]print(heapq.nlargest(3, list1)) # 查询最大的 3 个元素print(heapq.nsmallest(3, list1)) # 查询最小的 3 个元素
[43, 26, 23]
[-4, 1, 2]
12让字典保持有序#让字典保持有序from collections import OrderedDict
res = OrderedDict()res['a'] = 1res['b'] = 2res['c'] = 3for key in res:print(key, res[key])
a 1
b 2
c 3
13计算字典最大/小值# 计算字典最大/ 小值= {'pen': 10.0, 'ball': 98.5, 'banana': 15.6, 'computer': 3499, 'book': 52.3}min_price = min(zip(dict1.values(), dict1.keys()))print(min_price)max_price = max(zip(dict1.values(), dict1.keys()))print(max_price)
(10.0, 'pen')
(3499, 'computer')
14两个字典寻找相同点# 两个字典寻找相同点dict1 = {'a': 1, 'b': 2, 'c': 3}dict2 = {'b': 3, 'c': 3, 'd': 5}# 字典相同的键print(dict1.keys() & dict2.keys())# 字典不同的键print(dict1.keys() - dict2.keys())# 字典相同的键值对print(dict1.items() & dict2.items())
{'b', 'c'}
{'a'}
{('c', 3)}
15获取索引-键值对# 使用enumerate() 函数来获取索引-数值对str = 'Learning'for i, j in enumerate(str):print(i, j)
0 L
1 e
2 a
3 r
4 n
5 i
6 n
7 g
以上~
最后
以上就是听话含羞草为你收集整理的python统计单词个数_python | 实用语法技巧分享的全部内容,希望文章能够帮你解决python统计单词个数_python | 实用语法技巧分享所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复