0

不是吧?30秒 就能学会一个python小技巧?! - 小熊猫爱恰饭

 1 year ago
source link: https://www.cnblogs.com/xiaoxiongmao123/p/16438851.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

大家好鸭!我是小熊猫🖤

很多学习Python的朋友在项目实战中会遇到不少功能实现上的问题,有些问题并不是很难的问题,或者已经有了很好的方法来解决。当然,孰能生巧,当我们代码熟练了,自然就能总结一些好用的技巧,不过对于那些还在刚熟悉Python的同学可能并不会那么轻松。

本次给大家推荐一个学习这些技巧的很好的资源“30-seconds-of-python”,所有技巧方法只要30秒就能get到,完全可以利用业务时间不断积累。下面赶紧来看一下。

https://github.com/30-seconds/30-seconds-of-python

请添加图片描述

1.内容目录

下面是30秒学Python的整个目录,分为几大板块:List、Math、Object、String、Utility,以下是整理的思维脑图。

请添加图片描述

有什么python相关报错解答自己不会的、或者源码资料/模块安装/女装大佬精通技巧 都可以来这里:(https://jq.qq.com/?_wv=1027&k=2Q3YTfym)或者文末私号

我挑选了10个实用并很有意思的方法分享给大家,其余的感兴趣可以自行学习。

1. List:all_equal

功能实现:检验一个列表中的所有元素是否都一样。

解读:使用[1:] 和 [:-1] 来比较给定列表的所有元素。

def all_equal(lst):
  return lst[1:] == lst[:-1]
all_equal([1, 2, 3, 4, 5, 6]) # False
all_equal([1, 1, 1, 1]) # True

2. List:all_unique

功能实现:如果列表所有值都是唯一的,返回 True,否则 False

解读:在给定列表上使用集合set()去重,比较它和原列表的长度。

def all_unique(lst):python学习交流裙:660193417###
  return len(lst) == len(set(lst))
x = [1,2,3,4,5,6]
y = [1,2,2,3,4,5]
all_unique(x) # True
all_unique(y) # False
在这里插入图片描述

3. List:bifurcate

功能实现:将列表值分组。如果在filter的元素是True,那么对应的元素属于第一个组;否则属于第二个组。

解读:使用列表推导式和enumerate()基于filter元素到各组。

def bifurcate(lst, filter):
  return [
    [x for i,x in enumerate(lst) if filter[i] == True],
    [x for i,x in enumerate(lst) if filter[i] == False]
  ]
bifurcate(['beep', 'boop', 'foo', 'bar'], [True, True, False, True])

# [ ['beep', 'boop', 'bar'], ['foo'] ]

4. List:difference

功能实现:返回两个iterables间的差异。

解读:创建b的集合,使用a的列表推导式保留不在_b中的元素。

def difference(a, b):
  _b = set(b)
  return [item for item in a if item not in _b]
difference([1, 2, 3], [1, 2, 4]) # [3]

5. List:flatten

功能实现:一次性的整合列表。

解读:使用嵌套的列表提取子列表的每个值。

def flatten(lst):
  return [x for y in lst for x in y]
flatten([[1,2,3,4],[5,6,7,8]]) # [1, 2, 3, 4, 5, 6, 7, 8]
在这里插入图片描述

6. Math:digitize

功能实现:将一个数分解转换为个位数字。

解读:将n字符化后使用map()函数结合int完成转化

def digitize(n):
  return list(map(int, str(n)))
digitize(123) # [1, 2, 3]

7. List:shuffle

功能实现:将列表元素顺序随机打乱。

解读:使用Fisher-Yates算法重新排序列表元素。

from copy import deepcopy
from random import randint

def shuffle(lst):
  temp_lst = deepcopy(lst)
  m = len(temp_lst)
  while (m):
    m -= 1
    i = randint(0, m)
    temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m]
  return temp_lst
foo = [1,2,3]
shuffle(foo) # [2,3,1] , foo = [1,2,3]

8. Math:clamp_number

功能实现:将数字num钳在由a和b边界值规定的范围中。

解读:如果num落尽范围内,返回num;否则,返回范围内最接近的数字。

def clamp_number(num,a,b):
  return max(min(num, max(a,b)),min(a,b))
clamp_number(2, 3, 5) # 3
clamp_number(1, -1, -5) # -1
在这里插入图片描述

9. String:byte_size

功能实现:返回字符串的字节数。

解读:使用string.encode(‘utf-8’)解码给定字符串,返回长度。

def byte_size(string):
  return len(string.encode('utf-8'))
byte_size('😀') # 4
byte_size('Hello World') # 11

10. Math:gcd

功能实现:计算几个数的最大公因数。

解读:使用reduce()和math.gcd在给定列表上实现。

from functools import reduce
import math

def gcd(numbers):
  return reduce(math.gcd, numbers)
gcd([8,36,28]) # 4

以上就是30秒学python的各种小技巧。怎么样,对于一些常见操作是不是有了一些新的启发,除此之外,还有很多其它技巧可以慢慢学习,希望对各位读者有所帮助。

https://github.com/30-seconds/30-seconds-of-python

我是小熊猫,咱下篇文章再见啦(✿◡‿◡)

在这里插入图片描述

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK