Python中單個字符也屬于字符串類型,python不屬于字符串的是_【python cookbook】python過濾字符串中不屬于指定集合的字符...

 2023-10-21 阅读 25 评论 0

摘要:1 #!/usr/bin/python2 #-*- coding: utf-8 -*-34 #過濾字符串中不屬于指定集合的字符5Python中單個字符也屬于字符串類型。6 importstring78 #生成所有字符的可復用的字符串 它還可以作為一個翻譯表 指明無需翻譯9 allchars = string.maketrans('','')101

1 #!/usr/bin/python

2 #-*- coding: utf-8 -*-

3

4 #過濾字符串中不屬于指定集合的字符

5

Python中單個字符也屬于字符串類型。6 importstring7

8 #生成所有字符的可復用的字符串 它還可以作為一個翻譯表 指明無需翻譯

9 allchars = string.maketrans('','')10

11

12 defmakefilter(keep):13 """返回一個函數 此函數接受一個字符串為參數14 并返回字符串的一個部分拷貝15 次拷貝紙包含在keep中的字符 keep必須是一個普通字符"""

16

python map?17 #生成一個由所有不再keep中的字符組成的字符串:keep的補集 即要刪除的字符串

18 delchars=allchars.translate(allchars,keep)19 #生成并返回需要過濾的函數(作為閉包)

20

21 defthefilter(s):22 returns.translate(allchars,delchars)23 returnthefilter24

25 if __name__ == '__main__':26 just_vowels = makefilter('aeiouy')27 print just_vowels('four score and seven years ago')

輸出 ouoeaeeyeaao

python3。但此代碼的缺陷為只適用于普通字符 對于unicode字符不適用

unicode字符串的translate方法只需要一個參數 (一個序列活著一個映射) 并且根據字符串的碼值進行索引

碼值不是映射的鍵的字符會直接復制,不做改變

與每個字符對應的值必須是一個unicode字符串或者None

但這種方法對普通字符串不適用

******************************************以下為unicode版代碼

流暢的python?通常 使用dict 或著list 作為unicode 字符串的translate 方法參數,來翻譯或者刪除某些字符

但我們可以使用更好的辦法 --編寫一個簡單的實現

__getitem__(進行索引時會調用的特殊方法)方法的類

importsetsclassKeeper(obj):def __init__(self,keep):

self.keep=sets.Set(map(ord,keep))def __getitem__(self,n):if n not inself.keep:returnNonereturnunichr(n)def __call___(self,s):returnunicode(s).translate(self)

makefilter=Keeperif __name__ == '__main__':

python編程?just_vowels= makefilter('aeiouy')print just_vowels(u'four score and seven years ago')

首先使用__init__初始化keep

最后使用__call__回調即

u'four score and seven years ago'.translate(['a','e','i','o','u','y'])

map用法

map(function,?iterable,?...)

pythoncookbook?Apply?function?to every item of?iterable?and return a list of the results. If additional?iterable?arguments are passed,?function?must take that many arguments and is applied to the items from all iterables in parallel. If one iterable is shorter than another it is assumed to be extended with?None?items. If?function?is?None, the identity function is assumed; if there are multiple arguments,?map()?returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The?iterable?arguments may be a sequence or any iterable object; the result is always a list.

將函數func作用于給定序列的每個元素,并用一個列表來提供返回值;如果func為None,func表現為身份函數,返回一個含有每個序列中元素集合的n個元組的列表。

例如

>>> ls = [1,2,3,4]>>> print map(lambda x:x*3,ls)

[3, 6, 9, 12]

ord() ?jie接受長度為一的unicode字符串為參數 返回unicode碼值

python堆。>>> print ord('a')97

"雙下劃線" 開始的是私有成員,意思是只有類對象自己能訪問,連子類對象也不能訪問到這個數據。

__init__

__init__方法在類的一個對象被建立時,馬上運行。這個方法可以用來對你的對象做一些你希望的初始化。注意,這個名稱的開始和結尾都是雙下劃線。

__call__

Python中有一個有趣的語法,只要定義類型的時候,實現__call__函數,這個類型就成為可調用的。

python reload、換句話說,我們可以把這個類型的對象當作函數來使用,相當于 重載了括號運算符。

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/156544.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息