python统计单词个数_python | 实用语法技巧分享

介绍一些python 编程中的一些小技巧,可能有你知道的,可能也有你不知道的,确实是非常有技巧性了,希望能记下来用的时候能想起。

0 1字符串翻转

# 字符串翻转exam = 'hello world'print(exam[::-1])from functools import reduceprint(reduce(lambda x, y: y+x, exam))

dlrow olleh

dlrow olleh

8935e89ebf2bf578ee230b83a5e519c3.png0 2单词大小写

# 单词大小写exam3 = 'i love china'print(exam3.upper())    # 全大写print(exam3.capitalize())   # 字符串首字符大写print(exam3.title())    # 每个单词首字母大写

I LOVE CHINA

I love china

I Love China

8935e89ebf2bf578ee230b83a5e519c3.png0 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'

8935e89ebf2bf578ee230b83a5e519c3.png0 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]

8935e89ebf2bf578ee230b83a5e519c3.png0 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]

8935e89ebf2bf578ee230b83a5e519c3.png0 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]

8935e89ebf2bf578ee230b83a5e519c3.png0 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]

8935e89ebf2bf578ee230b83a5e519c3.png0 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)]

8935e89ebf2bf578ee230b83a5e519c3.png0 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)

'元素相同'

8935e89ebf2bf578ee230b83a5e519c3.png 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}

8935e89ebf2bf578ee230b83a5e519c3.png 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]

8935e89ebf2bf578ee230b83a5e519c3.png12让字典保持有序
#让字典保持有序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

8935e89ebf2bf578ee230b83a5e519c3.png13计算字典最大/小值
# 计算字典最大/ 小值= {'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')

8935e89ebf2bf578ee230b83a5e519c3.png14两个字典寻找相同点
# 两个字典寻找相同点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)}

8935e89ebf2bf578ee230b83a5e519c3.png15获取索引-键值对
# 使用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

8935e89ebf2bf578ee230b83a5e519c3.png

以上~

87f1c27f11e5c33d6768ba476194f83e.gif

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值