python之常用方法(精)

查找列表中出现最频繁的元素

使用 max() 函数可以快速查找出一个列表中出现频率最高的某个元素。

>>> a = [1, 2, 3, 4, 3, 4, 5, 4, 4, 2]
>>> b = max(set(a), key=a.count)
>>> b
4

统计列表中所有元素的出现次数

collections 是 Python 中的一个宝藏模块,它提供了很多特性。Counter 方法正好可以完美解决这个需求。

>>> from collections import Counter
>>>
>>> a = [1, 2, 3, 4, 3, 4, 5, 4, 4, 2]
>>> Counter(a)
Counter({4: 4, 2: 2, 3: 2, 1: 1, 5: 1})
 
 

key 来自一个列表,而 value 相同, 使用 fromkeys,那是相当的优雅

keys = ['a', 'b', 'c']
value = 100
d = dict.fromkeys(keys, value)
 
 

使用 product() 函数避免嵌套的Python循环

list_a = [1, 2020, 70]
list_b = [2, 4, 7, 2000]
list_c = [3, 70, 7]

for a in list_a:
    for b in list_b:
        for c in list_c:
            if a + b + c == 2077:
                print(a, b, c)
# 70 2000 7


使用product


from itertools import product list_a = [1, 2020, 70] list_b = [2, 4, 7, 2000] list_c = [3, 70, 7] for a, b, c in product(list_a, list_b, list_c): if a + b + c == 2077: print(a, b, c) # 70 2000 7

 

 
 列表推导式:

1、[x for x in data if condition]
此处if主要起条件判断作用,data数据中只有满足if条件的才会被留下,最终生成一个数据列表。

 

2、[exp1 if condition else exp2 for x in data]
此处if…else主要起赋值作用。当data中的数据满足if条件时,将其做exp1处理,否则按照exp2处理,最终生成一个数据列表。

 

 (1)使用列表推导式,生成1-10以内的所有偶数
    even = [i for i in range(1, 10 + 1) if i % 2 == 0]
    print(even)
    # 输出结果:[2, 4, 6, 8, 10]

  (2)使用列表推导式,生成1-10以内,如果是奇数,输出“奇”,如果是偶数,则输出"偶"

    result = ["" if i % 2 == 0 else "" for i in range(1, 10+1) ]
    print(result)
    # 输出结果:['', '', '', '', '', '', '', '', '', '']

 

 

合并字典的最简单方法

合并字典是日常 Python 编程中的一个常见需求。有很多方法可以做到这一点。但在Python3.9 之前,所有这些方法都很难看。

从 Python3.9 开始,我们终于得到了最优雅的字典合并方式——使用联合运算符。

article_author = {'数据STUDIO': '云朵君', 
                  '机器学习研习院': '小猴子'}
author_cities = {'云朵君': '江苏', 
                 '小猴子': '成都'}

info = article_author|author_cities
print(info)
#{'数据STUDIO': '云朵君', 
  '机器学习研习院': '小猴子', 
  '云朵君': '江苏', 
  '小猴子': '成都'}

 

 集合操作
# 交集
set1 & set2

# 并集
set1 | set2

# 差集
set1 - set2

# 补集
set1 ^ set2

 

生成a到z的字符串
1)
import string
letters = string.ascii_lowercase


2)

letters = "".join(map(chr, range(ord('a'), ord('z')+1)))

 

 列表中值求和
1. sum

num_list = list(range(1, 11))

sum(num_list)


2. reduce

from functools import reduce

total = reduce(lambda x,y : x + y, num_list)

 

列表中数值去重
def distFunc1(a):
    """使用集合去重"""
    a = list(set(a))
    print(a)

def distFunc2(a):
    """将一个列表的数据取出放到另一个列表中,中间作判断"""
    list = []
    for i in a:
        if i not in list:
            list.append(i)
    #如果需要排序的话用sort
    list.sort()
    print(list)

def distFunc3(a):
    """使用字典"""
    b = {}
    b = b.fromkeys(a)
    c = list(b.keys())
    print(c)

if __name__ == "__main__":
    a = [1,2,4,2,4,5,7,10,5,5,7,8,9,0,3]
    distFunc1(a)
    distFunc2(a)
    distFunc3(a)

 

 一个文本中,单词出现频率最高的10个单词
import re

# 方法一
def test(filepath):
    
    distone = {}

    with open(filepath) as f:
        for line in f:
            line = re.sub("\W+", " ", line)
            lineone = line.split()
            for keyone in lineone:
                if not distone.get(keyone):
                    distone[keyone] = 1
                else:
                    distone[keyone] += 1
    num_ten = sorted(distone.items(), key=lambda x:x[1], reverse=True)[:10]
    num_ten =[x[0] for x in num_ten]
    return num_ten
    
 
# 方法二 
# 使用 built-in 的 Counter 里面的 most_common
import re
from collections import Counter


def test2(filepath):
    with open(filepath) as f:
        return list(map(lambda c: c[0], Counter(re.sub("\W+", " ", f.read()).split()).most_common(10)))

 

 简单限流:
def is_action_allowed(user_id, action_key, period, max_count):
    key = 'hist:%s:%s' % (user_id, action_key)
    now_ts = int(time.time() * 1000)  # 毫秒时间戳
    with client.pipeline() as pipe:  # client 是 StrictRedis 实例
        # 记录行为
        pipe.zadd(key, {str(now_ts): now_ts})  # value 和 score 都使用毫秒时间戳
        # 移除时间窗口之前的行为记录,剩下的都是时间窗口内的
        pipe.zremrangebyscore(key, 0, now_ts - period * 1000)
        # 获取窗口内的行为数量
        pipe.zcard(key)
        # 设置 zset 过期时间,避免冷用户持续占用内存
        # 过期时间应该等于时间窗口的长度,再多宽限 1s
        pipe.expire(key, period + 1)
        # 批量执行
        _, _, current_count, _ = pipe.execute()
    # 比较数量是否超标
    return current_count <= max_count


if __name__ == '__main__':

    for i in range(20):
        print(is_action_allowed("lian", "reply", 60, 5))

 


 
posted @ 2022-11-04 17:46  X-Wolf  阅读(92)  评论(0编辑  收藏  举报