有哪些不为人知的Python技巧值得分享?

2026-04-30 20:001阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1140个文字,预计阅读时间需要5分钟。

有哪些不为人知的Python技巧值得分享?

本章节为家庭带来关于Python的相关知识,主要整理了冷门技巧的相关问题,包括first库、tqdm库、delattr、!cmd操作、this库等内容。下面一起来看一看,希望对大家有所帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要整理了冷门的技巧的相关问题,包括了first库、tqdm库、delattr、!cmd操作、this库等等内容,下面一起来看一下,希望对大家有帮助。

程序员必备接口测试调试工具:立即使用
Apipost = Postman + Swagger + Mock + Jmeter
Api设计、调试、文档、自动化测试工具
后端、前端、测试,同时在线协作,内容实时同步

first库

没错,就是first,这是个库的名称,目前124个stars

first is an MIT-licensed Python package with a simple function that returns the first true value from an iterable, or None if there is none. If you need more power, you can also supply a key function that is used to judge the truth value of the element or a default value if None doesn’t fit your use case.

简单来讲就是会返回第一个正确的可遍历对象。

如第一个例子,第一个正确的可遍历对象为`77`

from first import firstprint(first([0, None, False, 77,[], (), 42]))登录后复制

第二个例子用了re正则,我在其基础上进行改动,以便大家更容易理解。

import refrom first import first re1 = re.compile('(.)b(.*)')re2 = re.compile('a(.*)')# re1,re2换位置结果变化m = first(regexp.match('abcwerfwer') for regexp in [ re2,re1])print(m)if not m: print('no match!')elif m.re is re1: print('re1', m.group(1))elif m.re is re2: print('re2', m.group(1))#<re.Match object; span=(0, 10), match='abcwerfwer'>#re2 bcwerfwer登录后复制

re1,re2换位置结果变化

import refrom first import first re1 = re.compile('(.)b(.*)')re2 = re.compile('a(.*)')m = first(regexp.match('abcwerfwer') for regexp in [re1, re2])print(m)if not m: print('no match!')elif m.re is re1: print('re1', m.group(1))elif m.re is re2: print('re2', m.group(1))#<re.Match object; span=(0, 10), match='abcwerfwer'>#re1 a登录后复制

tqdm库

这是一个非常有趣的库,stars不算太多,但是可以给你平淡的代码生活中泛起一丝涟漪。
分享一段读取数据后并插入数据的代码,我想将数据插入到df2中,只需在range前加一步即可实现可视化,给你在枯燥的代码时光里带来一丝喜悦

from tqdm import tqdm# 还可以用以下办法是一个道理# from tqdm import trange# for i in trange(0,len(year),96):print(len(year))for i in tqdm(range(0,len(year),96)): # print(temp[i:i+96],len(temp[i:i+96])) try: df2.loc[index,3:99] = list(np.insert(df2.values[:,3:99], index, values=temp[i:i+96], axis=0)[index]) # print(temp[i:i+96]) # df.insert(1, '0:00', value=temp[i:i+96], allow_duplicates=True) # print(index,'+',len(year)) except Exception as e: pass index+=1登录后复制


delattr

python内置属性,用来删除class类中的属性,咱们以牛客网随机一道题为例

有哪些不为人知的Python技巧值得分享?

ListNode类中只有一个__init__属性,delattr函数就是人为删去此属性,在第一个a处会在控制台打印self.val的值,但下一个a处就会出现TypeError: ListNode() takes no arguments,这是因为属性__init__已经被删除,就不需要传入x值,所以出现报错

class ListNode: def __init__(self, x): self.val = x self.next = None print(self.val)class Solution: def reverseBetween(self , head: ListNode, m: int, n: int) -> ListNode: a = ListNode(1) delattr(ListNode, '__init__') a = ListNode(1)# 报错b= Solution()b.reverseBetween(1,2,3)登录后复制

!cmd操作

控制台输入!cmd可以直接进入命令提示符模式,spider和pycharm都可使用

this库

这个库恐怕00后全军覆没一首Python诗奉上

#分享一首诗给大家,每个版本都有import this登录后复制

以上就是总结分享Python冷门的技巧的详细内容,更多请关注自由互联其它相关文章!

本文共计1140个文字,预计阅读时间需要5分钟。

有哪些不为人知的Python技巧值得分享?

本章节为家庭带来关于Python的相关知识,主要整理了冷门技巧的相关问题,包括first库、tqdm库、delattr、!cmd操作、this库等内容。下面一起来看一看,希望对大家有所帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要整理了冷门的技巧的相关问题,包括了first库、tqdm库、delattr、!cmd操作、this库等等内容,下面一起来看一下,希望对大家有帮助。

程序员必备接口测试调试工具:立即使用
Apipost = Postman + Swagger + Mock + Jmeter
Api设计、调试、文档、自动化测试工具
后端、前端、测试,同时在线协作,内容实时同步

first库

没错,就是first,这是个库的名称,目前124个stars

first is an MIT-licensed Python package with a simple function that returns the first true value from an iterable, or None if there is none. If you need more power, you can also supply a key function that is used to judge the truth value of the element or a default value if None doesn’t fit your use case.

简单来讲就是会返回第一个正确的可遍历对象。

如第一个例子,第一个正确的可遍历对象为`77`

from first import firstprint(first([0, None, False, 77,[], (), 42]))登录后复制

第二个例子用了re正则,我在其基础上进行改动,以便大家更容易理解。

import refrom first import first re1 = re.compile('(.)b(.*)')re2 = re.compile('a(.*)')# re1,re2换位置结果变化m = first(regexp.match('abcwerfwer') for regexp in [ re2,re1])print(m)if not m: print('no match!')elif m.re is re1: print('re1', m.group(1))elif m.re is re2: print('re2', m.group(1))#<re.Match object; span=(0, 10), match='abcwerfwer'>#re2 bcwerfwer登录后复制

re1,re2换位置结果变化

import refrom first import first re1 = re.compile('(.)b(.*)')re2 = re.compile('a(.*)')m = first(regexp.match('abcwerfwer') for regexp in [re1, re2])print(m)if not m: print('no match!')elif m.re is re1: print('re1', m.group(1))elif m.re is re2: print('re2', m.group(1))#<re.Match object; span=(0, 10), match='abcwerfwer'>#re1 a登录后复制

tqdm库

这是一个非常有趣的库,stars不算太多,但是可以给你平淡的代码生活中泛起一丝涟漪。
分享一段读取数据后并插入数据的代码,我想将数据插入到df2中,只需在range前加一步即可实现可视化,给你在枯燥的代码时光里带来一丝喜悦

from tqdm import tqdm# 还可以用以下办法是一个道理# from tqdm import trange# for i in trange(0,len(year),96):print(len(year))for i in tqdm(range(0,len(year),96)): # print(temp[i:i+96],len(temp[i:i+96])) try: df2.loc[index,3:99] = list(np.insert(df2.values[:,3:99], index, values=temp[i:i+96], axis=0)[index]) # print(temp[i:i+96]) # df.insert(1, '0:00', value=temp[i:i+96], allow_duplicates=True) # print(index,'+',len(year)) except Exception as e: pass index+=1登录后复制


delattr

python内置属性,用来删除class类中的属性,咱们以牛客网随机一道题为例

有哪些不为人知的Python技巧值得分享?

ListNode类中只有一个__init__属性,delattr函数就是人为删去此属性,在第一个a处会在控制台打印self.val的值,但下一个a处就会出现TypeError: ListNode() takes no arguments,这是因为属性__init__已经被删除,就不需要传入x值,所以出现报错

class ListNode: def __init__(self, x): self.val = x self.next = None print(self.val)class Solution: def reverseBetween(self , head: ListNode, m: int, n: int) -> ListNode: a = ListNode(1) delattr(ListNode, '__init__') a = ListNode(1)# 报错b= Solution()b.reverseBetween(1,2,3)登录后复制

!cmd操作

控制台输入!cmd可以直接进入命令提示符模式,spider和pycharm都可使用

this库

这个库恐怕00后全军覆没一首Python诗奉上

#分享一首诗给大家,每个版本都有import this登录后复制

以上就是总结分享Python冷门的技巧的详细内容,更多请关注自由互联其它相关文章!