社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

震惊了!每 30 秒学会一个 Python 小技巧

数据分析与开发 • 4 年前 • 560 次点击  

很多学习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,以下是整理的思维脑图。

我挑选了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):
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'], [TrueTrueFalseTrue])
# [ ['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

- EOF -


推荐阅读  点击标题可跳转

1、推荐两个高效处理 Excel 的 Python 开源库

2、超全 Python IDE 武器库大总结,优缺点一目了然!

3、让数据动起来:Python动态图表制作!



看完本文有收获?请转发分享给更多人

推荐关注「数据分析与开发」,提升数据技能

点赞和在看就是最大的支持❤️

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/114111
 
560 次点击